HDU 2544 最短路
Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?
Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
输入保证至少存在1条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
Sample Output
3 2
Source
Recommend
lcy
Solution
三种解法(Floyed、Dijkstra、SPFA)求最短路
Code
1、Floyed:O(n^3)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#define L 110
#define inf 1000000009
#define LL long long
using namespace std;
int n, m, d[L][L], a, b, c, dis[L];
bool vis[L];
inline void Floyed() {
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
if (d[i][k] != inf)
for (int j = 1; j <= n; ++j)
if (d[i][j] > d[i][k] + d[j][k]) d[i][j] = d[j][i] = d[i][k] + d[j][k];
printf("%d\n", d[1][n]);
}
int main(){
freopen("HDU2544.in", "r", stdin);
freopen("HDU2544.out", "w", stdout);
while (scanf("%d %d", &n, &m) != EOF && n && m) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) d[i][j] = inf;
for (int i = 1; i <= m; ++i)
scanf("%d %d %d", &a, &b, &c), d[a][b] = d[b][a] = c;
Floyed();
}
return 0;
}
2、Dijkstra:O(n^2)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#define L 110
#define inf 1000000009
#define LL long long
using namespace std;
int n, m, d[L][L], a, b, c, dis[L];
bool vis[L];
inline void dijkstra() {
for (int i = 1; i <= n; ++i) dis[i] = d[1][i], vis[i] = false;
vis[1] = true;
for (int i = 1; i <= n; ++i) {
int temp, minx = inf;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dis[j] < minx) minx = dis[j], temp = j;
vis[temp] = true;
for (int j = 1; j <= n; ++j)
if (!vis[j] && d[j][temp] + dis[temp] < dis[j]) dis[j] = d[j][temp] + dis[temp];
}
printf("%d\n", dis[n]);
}
int main(){
freopen("HDU2544.in", "r", stdin);
freopen("HDU2544.out", "w", stdout);
while (scanf("%d %d", &n, &m) != EOF && n && m) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) d[i][j] = inf;
for (int i = 1; i <= m; ++i)
scanf("%d %d %d", &a, &b, &c), d[a][b] = d[b][a] = c;
dijkstra();
}
return 0;
}
3、SPFA:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#define L 10010
#define inf 1000000009
#define LL long long
using namespace std;
struct node {
int nxt, to, w;
} e[L << 1];
int n, m, d[L], head[L], a, b, c, cnt;
bool vis[L];
queue <int> q;
inline void add(int a, int b, int c) {
e[++cnt].nxt = head[a], e[cnt].to = b, e[cnt].w = c, head[a] = cnt;
}
int main(){
freopen("HDU2544.in", "r", stdin);
freopen("HDU2544.out", "w", stdout);
while (scanf("%d %d", &n, &m) != EOF && n && m) {
memset(head, 0, sizeof(head));
memset(vis, 0, sizeof(vis));
for (int i = 2; i <= n; ++i) d[i] = inf;
d[1] = 0, cnt = 0;
for (int i = 1; i <= m; ++i)
scanf("%d %d %d", &a, &b, &c), add(a, b, c), add(b, a, c);
while(!q.empty()) q.pop();
q.push(1);
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (d[y] > d[x] + e[i].w) {
d[y] = d[x] + e[i].w;
if (!vis[y]) vis[y] = 1, q.push(y);
}
}
}
printf("%d\n", d[n]);
}
return 0;
}
Summary
多组数据需要注意预处理