标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 760 Accepted Submission(s): 290
4 5
1 2 3
1 3 7
1 4 50
2 3 4
3 4 2
3 2
1 2 30
2 3 10
0 0
#include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include <ctime> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> using namespace std; const int INF=0x3f3f3f3f; const double eps=1e-10; const double PI=acos(-1.0); const int maxn=5000; int w[maxn][maxn]; int n, m; int v[maxn]; int d[maxn]; int num[maxn]; int dijkstra(int k) { memset(v, 0, sizeof(v)); v[k] = 1; for(int i = 1; i <= n; i++) d[i] = (i==1 ? 0 : INF); for(int i = 1; i <= n; i++) { int x, m = INF; for(int j = 1; j <= n; j++) if(!v[j] && d[j]<=m) m = d[x=j]; v[x]=1; for(int j = 1; j <= n; j++) d[j] = min(d[j], d[x] + w[x][j]); } return d[n]; } int main() { while(~scanf("%d%d", &n, &m)) { if(n==0 && m==0) break; memset(w, INF, sizeof(w)); int a, b, t; for(int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &t); w[a][b] = w[b][a] = min(t, w[a][b]); } //int ans = INF; int cnt = 0; memset(num, INF, sizeof(num)); for(int i = 2; i < n; i++) cnt = max(cnt, dijkstra(i)); if(cnt == INF) printf("Inf\n"); else printf("%d\n", cnt); } return 0; }
HDU5137 How Many Maos Does the Guanxi Worth(枚举+dijkstra)
标签:
原文地址:http://www.cnblogs.com/ZP-Better/p/4703194.html