UVA 567 - Risk(Flyod)

题目链接

还是有点裸的,看懂题意就OK了,第一次没有用EOF,超时。。。2Y,输入输出比较繁琐。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #define N 1000000

 4 int map[21][21];

 5 void floyd()

 6 {

 7     int i,j,k;

 8     for(k = 1; k <= 20; k ++)

 9         for(i = 1; i <= 20; i ++)

10             for(j = 1; j <= 20; j ++)

11             {

12                 if(map[i][j] > map[i][k] + map[k][j]&&map[i][k] != N&&map[k][j] != N)

13                     map[i][j] = map[i][k] + map[k][j];

14             }

15 }

16 int main()

17 {

18     int i,j,s,sv,ev,n,num = 0;

19     while(scanf("%d",&s)!=EOF)

20     {

21     for(i = 1; i <= 20; i ++)

22     {

23         for(j = 1; j <= 20; j ++)

24             map[i][j] = N;

25         map[i][i] = 0;

26     }

27     num ++;

28     for(i = 1; i <= 19; i ++)

29     {

30         if(i > 1)

31         scanf("%d",&s);

32         for(j = 1; j <= s; j ++)

33         {

34             scanf("%d",&ev);

35             map[i][ev] = 1;

36             map[ev][i] = 1;

37         }

38     }

39     floyd();

40     printf("Test Set #%d\n",num);

41     scanf("%d",&n);

42     for(i = 1; i <= n; i ++)

43     {

44         scanf("%d%d",&sv,&ev);

45         if(sv < 10)

46             printf(" %d",sv);

47         else

48             printf("%d",sv);

49         printf(" to ");

50         if(ev < 10)

51             printf(" %d: ",ev);

52         else

53             printf("%d: ",ev);

54         printf("%d\n",map[sv][ev]);

55     }

56     printf("\n");

57     }

58     return 0;

59 }

你可能感兴趣的:(uva)