hdu 2066 一个人的旅行
SPFA
#include<stdio.h> #include<math.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int maxn = 1111; int tt[maxn][maxn], ji[maxn], yy[maxn]; vector<int>abc[maxn]; struct qwe{ int node, time; }dt[maxn]; void chushi() { 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++) abc[i].clear(); for (i = 0; i<maxn; i++) ji[i] = 999999999; } bool cmp(const qwe&a, const qwe&b) { return a.time<b.time; } int main() { int T, S, D; while (~scanf("%d%d%d", &T, &S, &D)) { int i, j, u, v, time; chushi(); for (i = 0; i<T; i++) { scanf("%d%d%d", &u, &v, &time); if (time<tt[u][v]) { abc[u].push_back(v); tt[u][v] = time; abc[v].push_back(u); tt[v][u] = time; } } ji[0] = 0; int b = 0; for (i = 0; i<S; i++) { int q; scanf("%d", &q); abc[0].push_back(q); tt[0][q] = 0; tt[q][0] = 999999999; } for (i = 0; i<D; i++) { scanf("%d", &yy[i]); } dt[0].node = 0; dt[0].time = 0; ji[0] = 0; int uu; for (i = 0; i <= b; i++) { for (j = 0; j<abc[dt[i].node].size(); j++) { uu = tt[dt[i].node][abc[dt[i].node][j]]; if (dt[i].time + uu<ji[abc[dt[i].node][j]] && uu != 999999999) { b++; dt[b].node = abc[dt[i].node][j]; dt[b].time = dt[i].time + uu; ji[abc[dt[i].node][j]] = dt[i].time + uu; } } } int ans = 999999999; for (i = 0; i<D; i++) if (ji[yy[i]]<ans) ans = ji[yy[i]]; printf("%d\n", ans); } return 0; }