标签:des style os io for div amp 算法
3 3 1 2 1 2 3 1 1 3 1 3 3 1 2 1 1 2 3 2 3 1
3 It‘s impossible.
Floyd算法保证了在枚举第k个的时候,前k-1个点的dp[i][j]已经全部被计算,也就是说,不经过第k个点的dp[i][j]都已经算出,那么再求出从i到k的权值加上从j到k的权值加上dp[i][j],不断的更新这个值,就是最终的最小环。
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <stack> #define lson o<<1, l, m #define rson o<<1|1, m+1, r using namespace std; typedef long long LL; const int maxn = 105; const int MAX = 1000001; const int mod = 1000000007; int n, m; int dp[maxn][maxn], w[maxn][maxn]; int main() { while(~scanf("%d%d", &n, &m)) { for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) w[i][j] = MAX; for(int i = 0; i < m;i++){ int a, b, c; scanf("%d%d%d", &a, &b, &c); if(c < w[a][b]) w[a][b] = w[b][a] = c; } for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[i][j] = w[i][j]; int ans = MAX; for(int k = 1; k <= n; k++) { for(int i = 1; i < k; i++) for(int j = i+1; j < k; j++) ans = min( w[i][k]+w[k][j]+dp[i][j], ans ); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[i][j] = min( dp[i][j], dp[i][k]+dp[k][j] ); } if(ans != MAX) printf("%d\n", ans); else printf("It's impossible.\n"); } return 0; }
HDU 1599 find the mincost route (Floyd求最小环) >>,布布扣,bubuko.com
HDU 1599 find the mincost route (Floyd求最小环) >>
标签:des style os io for div amp 算法
原文地址:http://blog.csdn.net/u013923947/article/details/38492513