标签:des style blog io ar os sp for on
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <queue> using namespace std; int n, m, d[31][31]; int dis[31]; bool vis[31]; void Input() { memset(d, -1, sizeof(d)); int a, b, c; for (int i = 0; i < m; ++i) { scanf("%d%d%d", &a, &b, &c); if (d[a][b] == -1 || d[a][b] > c) d[a][b] = d[b][a] = c; } } int SPFA(int x) { memset(vis, 0, sizeof(vis)); memset(dis, -1, sizeof(dis)); dis[1] = 0; queue <int> q; q.push(1); int k; while (!q.empty()) { k = q.front(); q.pop(); vis[k] = 0; for (int i = 2; i <= n; ++i) { if (d[k][i] == -1 || i == x) continue; if (dis[i] == -1 || dis[i] > dis[k] + d[k][i]) { dis[i] = dis[k] + d[k][i]; if (vis[i] == 0) { q.push(i); vis[i] = 1; } } } } return dis[n]; } int qt() { int ans, v; for (int i = 2; i < n; ++i) { v = SPFA(i); if (v == -1) return -1; if (i == 2) ans = v; else ans = max(ans, v); } return ans; } int main() { //freopen("test.txt", "r", stdin); while (scanf("%d%d", &n, &m) != EOF && n && m) { Input(); int ans = qt(); if (ans == -1) printf("Inf\n"); else printf("%d\n", ans); } return 0; }
ACM学习历程——HDU5137 How Many Maos Does the Guanxi Worth(14广州10题)(单源最短路)
标签:des style blog io ar os sp for on
原文地址:http://www.cnblogs.com/andyqsmart/p/4162358.html