题目链接:UVa 567 - Risk
Floyd。
好恶心的输入输出。。
题目不难,不过刚开始Floyd那里搞得超过int范围了,加上判断(if(dis[i][k] == INF || dis[k][j] == INF))就好了。
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
const int MAX_N = 20 + 2;
const int INF = (1 << 30);
int dis[MAX_N][MAX_N];
int num,cnt;
void floyd()
{
for(int k = 1;k <= 20;k++)
{
for(int i = 1;i <= 20;i++)
{
for(int j = 1;j <= 20;j++)
{
if(dis[i][k] == INF || dis[k][j] == INF)
continue;
dis[i][j] = min(dis[i][j],dis[i][k] + dis[k][j]);
}
}
}
}
int main()
{
cnt = 0;
while(cin >> num)
{
for(int i = 1;i <= 20;i++)
{
for(int j = 1;j <= 20;j++)
{
if(i == j)
dis[i][j] = 0;
else
dis[i][j] = INF;
}
}
int temp;
for(int i = 0;i < num;i++)
{
cin >> temp;
dis[temp][1] = dis[1][temp] = 1;
}
for(int i = 2;i <= 19;i++)
{
cin >> num;
for(int j = 0;j < num;j++)
{
cin >> temp;
dis[temp][i] = dis[i][temp] = 1;
}
}
floyd();
cin >> num;
cout << "Test Set #" << ++cnt << endl;
int a,b;
for(int i = 0;i < num;i++)
{
cin >> a >> b;
cout << setw(2) << a;
cout << " to ";
cout << setw(2) << b;
cout << ": ";
cout << dis[a][b] << endl;
}
cout << endl;
}
return 0;
}