标签:route 道路 clu tle 技术 inpu esc 除了 现在
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599
1 #include<stdio.h> 2 #include<algorithm> 3 #define LL long long 4 #define mem(a, b) memset(a, b, sizeof(a)) 5 using namespace std; 6 const LL inf = 0x3f3f3f3f; 7 const int MAXN = 110; 8 9 int n, m; 10 LL dis[MAXN][MAXN], map[MAXN][MAXN];//最短路径 直接路径 11 LL ans; 12 13 void floyd() 14 { 15 ans = inf; 16 for(int k = 1; k <= n; k ++) //枚举中点 17 { 18 for(int i = 1; i < k; i ++) //起点 19 for(int j = i + 1; j < k; j ++)//终点 20 ans = min(ans, map[i][k] + map[k][j] + dis[i][j]); //在dis没更新倒k之前,是没有经过k点的。所以保证了至少3个不同的点 21 for(int i = 1; i <= n; i ++) 22 for(int j = 1; j <= n; j ++) 23 dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); 24 } 25 if(ans != inf) 26 printf("%lld\n", ans); 27 else 28 printf("It‘s impossible.\n"); 29 } 30 31 int main() 32 { 33 while(scanf("%d%d", &n, &m) != EOF) 34 { 35 for(int i = 1; i <= n; i ++) 36 for(int j = 1; j <= n; j ++) 37 { 38 if(i == j) 39 dis[i][j] = map[i][j] = 0; 40 else 41 dis[i][j] = map[i][j] = inf; 42 } 43 for(int i = 1; i <= m; i ++) 44 { 45 int a, b; 46 LL c; 47 scanf("%d%d%lld", &a, &b, &c); 48 dis[a][b] = dis[b][a] = map[a][b] = map[b][a] = min(map[a][b], c);//双向边 49 } 50 floyd(); 51 } 52 return 0; 53 }
find the mincost route【无向图最小环】
标签:route 道路 clu tle 技术 inpu esc 除了 现在
原文地址:https://www.cnblogs.com/yuanweidao/p/11517084.html