洛谷 1339 最短路
洛谷 1339 最短路
裸的最短路问题,,没什么可说的,当作是复习了个模板吧
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 const int maxn = 6000; 6 const int maxm = 50000; 7 int last[maxn], pre[maxm], other[maxm], len[maxm]; 8 int tot = 0; 9 int dis[maxn], que[maxn], vis[maxn]; 10 int n, m, ts, te; 11 int x, y, z; 12 void spfa(int s) { 13 memset(dis, 127, sizeof(dis)); 14 memset(que, 0, sizeof(que)); 15 dis[s] = 0; 16 que[1] = s; 17 int h = 0, t = 1; 18 while (h < t) { 19 h = (h + 1) % maxn; 20 int cur = que[h]; 21 vis[cur] = 0; 22 for (int p = last[cur]; p; p = pre[p]) { 23 int q = other[p]; 24 if (dis[q] > dis[cur] + len[p]) { 25 dis[q] = dis[cur] + len[p]; 26 if (vis[q] == 0) { 27 vis[q] = 1; 28 t = (t + 1) % maxn; 29 que[t] = q; 30 } 31 } 32 } 33 } 34 } 35 void add(int x, int y, int z) { 36 tot++; 37 pre[tot] = last[x]; 38 last[x] = tot; 39 other[tot] = y; 40 len[tot] = z; 41 } 42 43 int main () { 44 scanf("%d %d %d %d", &n, &m, &ts, &te); 45 for (int i = 1; i <= m; i++) { 46 scanf("%d %d %d", &x, &y, &z); 47 add(x, y, z); 48 add(y, x, z); 49 } 50 spfa(ts); 51 printf("%d", dis[te]); 52 53 54 return 0; 55 }