hdu-2544-最短路(Bellman-Ford算法模板)
题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa
1 /* 2 Name:HDU-2544-最短路 3 Copyright: 4 Author: 5 Date: 2018/4/17 10:34:47 6 Description: 7 man-ford 8 */ 9 #include <cstring> 10 #include <cstdio> 11 #include <iostream> 12 using namespace std; 13 const int MAXN = 105; 14 const int INF = 0x3f3f3f3f; 15 int dis[MAXN], g[MAXN][MAXN], N, M; 16 bool v[MAXN]; 17 void bellman_ford() 18 { 19 int i,j; 20 memset(dis, 0x3f, sizeof(dis)); 21 dis[1]=0; 22 bool update=true; 23 while(update){ 24 update=false; 25 for(i=1;i<=N;++i){ 26 for(j=1;j<=N;++j){ 27 if(g[i][j]!=INF&&dis[j]>dis[i]+g[i][j]){ 28 dis[j]=dis[i]+g[i][j]; 29 update=true; 30 } 31 } 32 } 33 if (!update) return;//优化 34 } 35 } 36 int main() 37 { 38 // freopen("in.txt", "r", stdin); 39 while (~scanf("%d %d", &N, &M) && (N+M)) { 40 memset(g, 0x3f, sizeof(g)); 41 for (int i=0; i<M; i++) { 42 int a, b, c; 43 scanf("%d %d %d", &a, &b, &c); 44 if (g[a][b] > c) { 45 g[a][b] = g[b][a] = c; 46 } 47 } 48 bellman_ford(); 49 printf("%d\n", dis[N]); 50 } 51 return 0; 52 }