题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2544
思路
最短路算法模板题,求解使用的Dijkstra算法、Floyd算法、SPFA算法可以当做求解最短路问题的模板使用。
代码
Dijkstra算法:
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 0x3f3f3f; 8 const int N = 100 + 10; 9 int map[N][N]; 10 int dist[N]; 11 int visit[N]; 12 int n, m; 13 14 void dijkstra() 15 { 16 memset(visit, 0, sizeof(visit)); 17 for (int i = 1; i <= n; i++) 18 dist[i] = map[1][i]; 19 dist[1] = 0; 20 int min_dist, now; 21 for (int i = 1; i <= n; i++) 22 { 23 min_dist = INF; 24 for (int j = 1; j <= n; j++) 25 { 26 if (!visit[j] && dist[j] < min_dist) 27 { 28 min_dist = dist[j]; 29 now = j; 30 } 31 } 32 if (min_dist == INF) break; 33 visit[now] = 1; 34 for (int j = 1; j <= n; j++) //“松弛”操作 35 { 36 if (dist[now] + map[now][j] < dist[j]) 37 dist[j] = dist[now] + map[now][j]; 38 } 39 } 40 printf("%d\n", dist[n]); 41 } 42 43 int main() 44 { 45 //freopen("hdoj2544.txt", "r", stdin); 46 while (scanf("%d%d", &n, &m) == 2 && n) 47 { 48 memset(map, INF, sizeof(map)); 49 int a, b, c; 50 for (int i = 0; i < m; i++) 51 { 52 scanf("%d%d%d", &a, &b, &c); 53 map[a][b] = map[b][a] = c; 54 } 55 dijkstra(); 56 } 57 return 0; 58 }
Floyd算法:
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 0x3f3f3f; 8 const int N = 100 + 10; 9 int map[N][N]; 10 int n, m; 11 12 void floyd() 13 { 14 for (int k = 1; k <= n; k++) 15 for (int i = 1; i <= n; i++) 16 for (int j = 1;j <= n; j++) 17 map[i][j] = min(map[i][j], map[i][k] + map[k][j]); 18 printf("%d\n", map[1][n]); 19 } 20 21 int main() 22 { 23 //freopen("hdoj2544.txt", "r", stdin); 24 while (scanf("%d%d", &n, &m) == 2 && n) 25 { 26 memset(map, INF, sizeof(map)); 27 int a, b, c; 28 for (int i = 0; i < m; i++) 29 { 30 scanf("%d%d%d", &a, &b, &c); 31 map[a][b] = map[b][a] = c; 32 } 33 floyd(); 34 } 35 return 0; 36 }
SPAF算法:
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <queue> 6 #include <vector> 7 using namespace std; 8 9 struct Edge 10 { 11 int s, e, dist; //边的起点、终点、长度 12 13 Edge() {} 14 Edge(int s, int e, int d) :s(s), e(e), dist(d) {} 15 }; 16 17 const int INF = 0x3f3f3f; 18 const int N = 100 + 10; 19 vector<Edge> v[N]; //使用邻接表存储图 20 int dist[N]; 21 int visit[N]; 22 int n, m; 23 24 void spfa(int s) 25 { 26 queue<int> q; 27 memset(dist, INF, sizeof(dist)); 28 memset(visit, 0, sizeof(visit)); 29 q.push(s); 30 visit[s] = 1; 31 dist[s] = 0; 32 33 while (!q.empty()) 34 { 35 int s = q.front(); 36 q.pop(); 37 visit[s] = 0; 38 for (int i = 0; i < v[s].size(); i++) 39 { 40 int e = v[s][i].e; 41 if (dist[e] > dist[s] + v[s][i].dist) 42 { 43 dist[e] = dist[s] + v[s][i].dist; 44 if (visit[e] == 0) 45 { 46 visit[e] = 1; 47 q.push(e); 48 } 49 } 50 } 51 } 52 printf("%d\n", dist[n]); 53 } 54 55 int main() 56 { 57 //freopen("hdoj2544.txt", "r", stdin); 58 while (scanf("%d%d", &n, &m) == 2 && n) 59 { 60 for (int i = 1;i <= n; i++) 61 v[i].clear(); 62 int a, b, c; 63 for (int i = 0; i < m; i++) 64 { 65 scanf("%d%d%d", &a, &b, &c); 66 v[a].push_back(Edge(a, b, c)); 67 v[b].push_back(Edge(b, a, c)); 68 } 69 spfa(1); //求结点1到其余各点的最短路径 70 } 71 return 0; 72 }