题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544
最近复习了最短路径的算法,就写了4个版本的测试。正好是模板题,就果断A之。。。
Dijkstar版本:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<climits> #include<queue> #include<algorithm> using namespace std; #define N 110 #define MAX 999999 #define CLR(arr, what) memset(arr, what, sizeof(arr)) int nodenum, edgenum; int map[N][N], dis[N]; bool visit[N]; int Dijkstra(int src, int des) { int temp, k; CLR(visit, false); for(int i = 1; i <= nodenum; ++i) dis[i] = (i == src ? 0 : map[src][i]); visit[src] = true; dis[src] = 0; for(int i = 1; i<= nodenum; ++i) { temp = MAX; for(int j = 1; j <= nodenum; ++j) if(!visit[j] && temp > dis[j]) temp = dis[k = j]; if(temp == MAX) break; visit[k] = true; for(int j = 1; j <= nodenum; ++j) if(!visit[j] && dis[j] > dis[k] + map[k][j]) dis[j] = dis[k] + map[k][j]; } return dis[des]; } int main() { int start, end, cost; int answer; while(~scanf("%d%d", &nodenum, &edgenum) && (nodenum + edgenum)) { for(int i = 1; i <= nodenum; ++i) for(int j = 1; j <= nodenum; ++j) map[i][j] = MAX; for(int i = 1; i <= edgenum; ++i) { scanf("%d%d%d", &start, &end, &cost); if(cost < map[start][end]) map[start][end] = map[end][start] = cost; } answer = Dijkstra(1, nodenum); printf("%d\n", answer); } return 0; }
Bellman_Ford版本:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<climits> #include<queue> #include<algorithm> using namespace std; #define N 110 #define MAX 999999 #define CLR(arr, what) memset(arr, what, sizeof(arr)) int nodenum, edgenum; int map[N][N], dis[N]; bool visit[N]; struct Edge { int u, v; int cost; }e[N * N / 2]; int Bellman_ford(int src, int des) { for(int i = 1; i <= nodenum; ++i) dis[i] = MAX; dis[src] = 0; for(int i = 0; i < nodenum - 1; ++i) //n-1遍 for(int j = 0; j < edgenum * 2; ++j) //each edge if(dis[e[j].v] > dis[e[j].u] + e[j].cost) dis[e[j].v] = dis[e[j].u] + e[j].cost; return dis[des]; } int main() { int start, end, cost; int answer; while(~scanf("%d%d", &nodenum, &edgenum) && (nodenum + edgenum)) { for(int i = 0; i < edgenum; ++i) { scanf("%d%d%d", &start, &end, &cost); //双向边 e[i * 2].u = start, e[i * 2].v = end, e[i * 2].cost = cost; e[i * 2 + 1].u = end, e[i * 2 + 1].v = start, e[i * 2 + 1].cost = cost; } answer = Bellman_ford(1, nodenum); printf("%d\n", answer); } return 0; }
Floyd版本:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<climits> #include<queue> #include<algorithm> using namespace std; #define N 110 #define MAX INT_MAX >> 1 #define CLR(arr, what) memset(arr, what, sizeof(arr)) int nodenum, edgenum; int map[N][N], dis[N]; bool visit[N]; int Floyd(int src, int des) //多源多汇最短路 { for(int k = 1; k <= nodenum; ++k) for(int i = 1; i <= nodenum; ++i) for(int j = 1; j <= nodenum; ++j) map[i][j] = min(map[i][j], map[i][k] + map[k][j]); return map[src][des]; } int main() { int start, end, cost; int answer; while(~scanf("%d%d", &nodenum, &edgenum) && (nodenum + edgenum)) { for(int i = 1; i <= nodenum; ++i) for(int j = 1; j <= nodenum; ++j) map[i][j] = MAX; for(int i = 0; i < edgenum; ++i) { scanf("%d%d%d", &start, &end, &cost); if(cost < map[start][end]) map[start][end] = map[end][start] = cost; } answer = Floyd(1, nodenum); printf("%d\n", answer); } return 0; }
SPFA版本:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<climits> #include<queue> #include<algorithm> using namespace std; #define N 110 #define MAX INT_MAX >> 1 #define CLR(arr, what) memset(arr, what, sizeof(arr)) int nodenum, edgenum; int map[N][N], dis[N]; bool visit[N]; int SPFA(int src, int des) { queue<int> q; CLR(visit, false); for(int i = 1;i <= nodenum; ++i) dis[i] = MAX; dis[src] = 0; visit[src] = true; q.push(src); while(!q.empty()) { int cur = q.front(); q.pop(); visit[cur] = false; //出队标记为false for(int i = 1; i <= nodenum; ++i) { if(dis[i] > dis[cur] + map[cur][i]) //没有2个集合,和Dijkstra有本质区别 { dis[i] = dis[cur] + map[cur][i]; //能松弛就松弛 if(!visit[i]) //不在队列中则加入,然后更新所有以前经过此点的最短路径 { q.push(i); visit[i] = true; } } } } return dis[des]; } int main() { int start, end, cost; int answer; while(~scanf("%d%d", &nodenum, &edgenum) && (nodenum + edgenum)) { for(int i = 1; i <= nodenum; ++i) for(int j = 1; j <= nodenum; ++j) map[i][j] = MAX; for(int i = 0; i < edgenum; ++i) { scanf("%d%d%d", &start, &end, &cost); if(cost < map[start][end]) map[start][end] = map[end][start] = cost; } answer = SPFA(1, nodenum); printf("%d\n", answer); } return 0; }