题目链接:https://vjudge.net/contest/66569#problem/A
题目:
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100Sample Output
90Hint
// // Created by hanyu on 2019/7/14. // #include<iostream> #include<algorithm> #include<queue> #include<map> #include<cstring> #include<cstdio> using namespace std; typedef long long ll; #define MAX 0x3f3f3f3f int T,n; const int maxn=1005; bool book[maxn]; int lu[maxn][maxn]; int d[maxn]; void spfa(int a) { int now; memset(book,false,sizeof(book)); memset(d,MAX,sizeof(d)); queue<int>qu; d[a]=0; book[a]=true; qu.push(a); while(!qu.empty()) { now= qu.front(); qu.pop(); book[now]=false; for(int i=1;i<=n;i++) { if(d[i]>d[now]+lu[now][i]) { d[i]=d[now]+lu[now][i]; if(!book[i]) { qu.push(i); book[i]=true; } } } } } int main() { while(~scanf("%d%d",&T,&n)) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { if (i == j) lu[i][j] = 0; else lu[i][j] = lu[j][i] = MAX; } } int x, y, z; for (int i = 1; i <= T; i++) { scanf("%d%d%d", &x, &y, &z); if(z<lu[x][y]) lu[x][y] = lu[y][x] = z; } spfa(1); printf("%d\n", d[n]); } return 0; }
[kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home(spfa算法)
原文:https://www.cnblogs.com/Vampire6/p/11201337.html