【ZOJ 1221】Risk

题意

给你20个城市的相邻关系,求给定任意两个城市的最短距离

分析

求任意两个城市最短距离,就是用floyd算法我脑残忘记了k是写在最外层的。

代码

#include<stdio.h>
#include<algorithm>
#define N 22
using namespace std;
int n,x,a,b,test,d[N][N],from,to;

void read()
{
    for(int j=1; j<=x; j++)
    {
        scanf("%d",&b);
        d[a][b]=d[b][a]=1;
    }
}
int main()
{
    while(~scanf("%d",&x))
    {
        test++;
        for(int i=1; i<=20; i++)for(int j=1; j<=20; j++)d[i][j]=50;
        a=1;read();
        for(a=2; a<20; a++)
        {
            scanf("%d",&x);
            read();
        }
        for(int k=1; k<=n; k++)//k写在外面
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                    d[i][j]=min(d[i][j],d[i][k]+d[k][j]);

        scanf("%d",&n);
        printf("Test Set #%d
",test);
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&from,&to);
            printf("%d to %d: %d
",from,to,d[from][to]);
        }
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/flipped/p/5188499.html