再做一遍floyed

#include<bits/stdc++.h>
#define R register int
using namespace std;
const int inf=0x3f3f3f3f;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') if(ch=='-') f=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
int n,m,k,a[201][201];
int main()
{
    n=read();m=read();k=read();
    for(R i=1;i<=n;i++)
     for(R j=1;j<=n;j++)
    {
        if(i==j) a[i][j]=0;
        else a[i][j]=inf;
    }
    for(R i=1;i<=m;i++)
    {
        int x=read(),y=read(),z=read();
        a[x][y]=min(a[x][y],z);//存在重边
    }
    for(R k=1;k<=n;k++)
     for(R i=1;i<=n;i++)
      for(R j=1;j<=n;j++)
       a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
    for(R i=1;i<=k;i++)
    {
        int x=read(),y=read();
        if(a[x][y]>=inf/2) printf("impossible\n");
        else printf("%d\n",a[x][y]);
    }
    return 0;
}

 

posted @ 2019-11-04 13:16  Gold_stein  阅读(140)  评论(0编辑  收藏  举报