2017.11.8. Floyed求最小环

版权声明:本文为博主原创文章,未经博主允许你想转还是转吧总之我这点东西还有版权这说法吗呵呵呵呵…… https://blog.csdn.net/AVALON_X/article/details/78497381

Floyed求最小环

适合题型:

1.求最小环

样题: HDU1599

std.cpp:

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;

inline int read()
{
    int x=0,w=1;char ch=0;
    while(ch< '0'||'9' <ch){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*w;
}

typedef long long ll;
inline ll red()
{
    ll x=0,w=1;char ch=0;
    while(ch< '0'||'9' <ch){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*w;
}

#define maxx 2000
inline char *get_str(char *str)
{
    fgets(str,maxx,stdin);
    if(str[strlen(str)-1]=='\n')
        str[strlen(str)-1]='\0';
    return str;
}

int n,m,u,v,val;

int f[105][105],dis[105][105];

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)   
            for(int j=1;j<=n;j++)
                f[i][j]=1e8,dis[i][j]=1e8;

        for(int i=1;i<=m;i++)
        {
            u=read();v=read();val=read();
            f[u][v]=f[v][u]=dis[u][v]=dis[v][u]=min(val,f[u][v]);
        }


        int ans=1e8;
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<k;i++)
                for(int j=i+1;j<k;j++)
                    ans=min(ans,dis[i][j]+f[i][k]+f[k][j]);

            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);   
        }
        if(ans==1e8)cout<<"It's impossible."<<endl;
        else    cout<<ans<<endl;
    }
    return 0;
}
展开阅读全文

没有更多推荐了,返回首页