| Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
3 3 1 2 1 2 3 1 1 3 1 3 3 1 2 1 1 2 3 2 3 1
Sample Output
3 It‘s impossible.
Source
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define ll long long
const ll INF = 1<<30;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
int n, m;
const int MAXN = 110;
ll g[MAXN][MAXN];
ll dist[MAXN][MAXN];
int Floyd()
{
ll mincircle = INF;
for(int k = 1; k <= n; k++)
{ //<span><span class="comment">求最小环权值</span></span>
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i!=j && i!=k && j!=k && mincircle>dist[i][j]+g[i][k]+g[k][j])
mincircle = dist[i][j]+g[i][k]+g[k][j];
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(dist[i][j] > dist[i][k]+dist[k][j])
dist[i][j] = dist[i][k]+dist[k][j];
}
}
}
return mincircle;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(cin>>n>>m)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
g[i][j] = (i==j)?0:INF;
}
}
int p, q;
ll w;
for(int i = 1; i <= m; i++)
{
scanf("%d %d %lld", &p, &q, &w);
if(g[p][q] > w)
g[p][q] = g[q][p] = w;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
dist[i][j] = g[i][j];
}
}
cout<<INF<<endl;
ll ans = Floyd();
if(ans != INF)
printf("%lld\n", ans);
else
printf("It's impossible.\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU - 1599 find the mincost route(Floyd求最小环)
原文地址:http://blog.csdn.net/u014028317/article/details/47668245