/***
FloydWarshall Minimum Cost
UVA - 567 - Risk
Author : Md.Saiful Islam Saif
***/
#include<stdio.h>
#define sz 21
#define INF 32000
int Cost[sz][sz];
int min( int x, int y)
{
if(x>y) return y;
return x;
}
void FLOYD_WARSHALL()
{
int i, j, k;
for(k=1;k<=20;k++)
{
for(i=1;i<=20;i++)
{
for(j=1;j<=20;j++)
{
Cost[i][j] = min(Cost[i][j], Cost[i][k]+Cost[k][j]);
}
}
}
}
int main()
{
int i, j, N, start, end , T=0, Node, temp ;
while(scanf("%d",&Node) == 1)
{
for(i=0;i<sz;i++)
for(j=0;j<sz;j++) Cost[i][j] = INF;//initial
for(i=1;i<20;i++)
{
if(i>1) scanf("%d",&Node);
for(j=1;j<=Node;j++)
{
scanf( "%d",&temp );
Cost[i][temp] = 1;
Cost[temp][i] = 1;
}
}
FLOYD_WARSHALL();
scanf("%d",&N);
printf("Test Set #%d\n",++T);
for(i=0;i<N;i++)
{
scanf("%d%d",&start, &end);
printf("%2d to %2d: %d\n\n",start,end,Cost[start][end]);
}
}
return 0;
}
/*
Input:
1 3
2 3 4
3 4 5 6
1 6
1 7
2 12 13
1 8
2 9 10
1 11
1 11
2 12 17
1 14
2 14 15
2 15 16
1 16
1 19
2 18 19
1 20
1 20
5
1 20
2 9
19 5
18 19
16 20
4 2 3 5 6
1 4
3 4 10 5
5 10 11 12 19 18
2 6 7
2 7 8
2 9 10
1 9
1 10
2 11 14
3 12 13 14
3 18 17 13
4 14 15 16 17
0
0
0
2 18 20
1 19
1 20
6
1 20
8 20
15 16
11 4
7 13
2 16
Output:
Test Set #1
1 to 20: 7
2 to 9: 5
19 to 5: 6
18 to 19: 2
16 to 20: 2
Test Set #2
1 to 20: 4
8 to 20: 5
15 to 16: 2
11 to 4: 1
7 to 13: 3
*/
No comments:
Post a Comment