UVA 10305 - Ordering Tasks(变形的Floyd)

题目链接

和以前做的一个排名(至今未作出)还相似。。。。上班结束回宿舍。。。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #define      N 10000000

 4 int p[101][101],o[101];

 5 int main()

 6 {

 7     int i,j,k,n,m,sv,ev,z;

 8     while(scanf("%d%d",&n,&m)!=EOF)

 9     {

10         memset(o,0,sizeof(o));

11         if(n == 0&&m == 0)break;

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

13         {

14             for(j = 1;j <= n;j ++)

15             {

16                 p[i][j] = N;

17             }

18             p[i][i] = 0;

19         }

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

21         {

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

23             p[sv][ev] = 1;

24         }

25         for(k = 1;k <= n;k ++)

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

27                 for(j = 1;j <= n;j ++)

28                 {

29                     if(p[i][j] > p[i][j]+p[j][k]&&p[i][j] != N && p[j][k] != N)

30                         p[i][j] = p[i][j]+p[j][k];

31                 }

32                 for(k = 1;k <= n;k ++)

33                 {

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

35                     {

36                         z = 0;

37                         if(o[i] == 0)

38                         {

39                             for(j = 1;j <= n;j ++)

40                             {

41                                 if(p[j][i] != N&&o[j] == 0&&i != j)

42                                 {

43                                     z = 1;

44                                     break;

45                                 }

46                             }

47                             if(!z) 

48                             {

49                                 o[i] = 1;

50                                 if(k == n)

51                                     printf("%d\n",i);

52                                 else

53                                     printf("%d ",i);

54                                 break;

55                             }

56                         }

57                     }

58                 }

59     }

60     return 0;

61 }

 

 

你可能感兴趣的:(order)