BFS.....
#include<stdio.h> #include<math.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int maxn = 1111; vector<int>abc[maxn]; int ji[maxn], tt[maxn][maxn], yy[maxn]; struct aaa{ int node, time; }dt[maxn*maxn]; bool cmp(const aaa&a, const aaa&b){ return a.time < b.time; } int main() { int n, m, mubiao; while (~scanf("%d%d%d", &n, &m, &mubiao)) { int i, j; for (i = 0; i < maxn; i++) for (j = 0; j < maxn; j++) tt[i][j] = 999999999; for (i = 0; i < maxn; i++) ji[i] = 999999999, abc[i].clear(); for (i = 0; i < m; i++) { int u, v, t; scanf("%d%d%d", &u, &v, &t); if (t < tt[v][u]) { tt[v][u] = t; abc[v].push_back(u); } } int dd; scanf("%d", &dd); for (i = 0; i < dd; i++) scanf("%d", &yy[i]); int b = 0; dt[0].node = mubiao; dt[0].time = 0; for (i = 0; i <= b; i++) { for (j = 0; j < abc[dt[i].node].size(); j++) { if (dt[i].time + tt[dt[i].node][abc[dt[i].node][j]] < ji[abc[dt[i].node][j]]) { b++; dt[b].node = abc[dt[i].node][j]; dt[b].time = dt[i].time + tt[dt[i].node][abc[dt[i].node][j]]; ji[abc[dt[i].node][j]] = dt[i].time + tt[dt[i].node][abc[dt[i].node][j]]; } } } int ans = 999999999; for (i = 0; i < dd; i++) if (ji[yy[i]] < ans) ans = ji[yy[i]]; if (ans == 999999999) printf("-1\n"); else printf("%d\n", ans); } return 0; }
时间: 2024-10-26 12:17:16