您的位置:首页 > 其它

【ZOJ 1221】Risk

2016-02-14 00:53 471 查看


题意

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

分析

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

代码

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

,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\n",test);
for(int i=1; i<=n; i++)
{
scanf("%d%d",&from,&to);
printf("%d to %d: %d\n",from,to,d[from][to]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: