floyd模板

来源:互联网 发布:java me模拟器 编辑:程序博客网 时间:2024/06/07 06:29
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;typedef long long LL;const int MAXN=1e3+5;const int INF=0x3f3f3f3f;int dist[MAXN][MAXN];void init(int n){    memset(dist,INF,sizeof(dist));    for(int i=0;i<n;i++) dist[i][i]=0;}void floyd(int n){    for(int k=0;k<n;k++){        for(int i=0;i<n;i++){            for(int j=0;j<n;j++){                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);            }        }    }}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        init(n);        int u,v,w;        for(int i=1;i<=m;i++)        {            scanf("%d%d%d",&u,&v,&w);            dist[u][v]=min(dist[u][v],w);            dist[v][u]=min(dist[v][u],w);        }        int st,ed;        scanf("%d%d",&st,&ed);        floyd(n);        if(dist[st][ed]!=INF) printf("%d\n",dist[st][ed]);        else printf("-1\n");    }    return 0;}