首页 > 代码库 > hdu 1599 floyd 最小环

hdu 1599 floyd 最小环

floyd真的是水很深啊 各种神奇

 

#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<stack>#define mem(a,b) memset(a,b,sizeof(a))#define ll __int64#define MAXN 1000#define INF 20000000#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int d[200][200];int g[200][200];int ans,n,m;void floyd(){    int i,j,k;    for(k=1;k<=n;k++)    {        for(i=1;i<k;i++)        {            for(j=i+1;j<k;j++)            {                ans=min(ans,g[i][k]+g[k][j]+d[i][j]);            }        }        for(i=1;i<=n;i++)        {            for(j=1;j<=n;j++)            {                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);            }        }    }}int main(){    int i,j;    int a,b,c;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(i=0;i<=n;i++)        {            for(j=0;j<=n;j++)            {                if(i==j) d[i][j]=g[i][j]=0;                else                         d[i][j]=g[i][j]=INF;            }        }        while(m--)        {            scanf("%d%d%d",&a,&b,&c);            if(d[a][b]>c)            d[a][b]=d[b][a]=g[a][b]=g[b][a]=c;        }        ans=INF;        floyd();        if(ans==INF) printf("It‘s impossible.\n");        else            printf("%d\n",ans);    }    return 0;}