1837. Isenbaev's Number(floyd)

1837

被数据结构部分打击的不行了 换地 刷点简单的 图论第一题 floyd水过

 1 #include <iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<algorithm>

 5 #include<stdlib.h>

 6 #include<string>

 7 #include<map>

 8 using namespace std;

 9 int w[310][310];

10 #define INF 0xfffffff

11 map<string,int>f;

12 struct node

13 {

14     char s[22];

15     int id;

16 }p[310];

17 bool cmp(node a,node b)

18 {

19     return strcmp(a.s,b.s)<0;

20 }

21 int main()

22 {

23     int i,j,n,k,g=0,u=0;

24     char s1[22],s2[22],s3[22];

25     for(i = 0 ; i <= 300 ; i++)

26         for(j = 0 ; j <= 300 ; j++)

27         w[i][j] = INF;

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

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

30     {

31         scanf("%s%s%s",s1,s2,s3);

32         if(!f[s1])

33         {

34             g++;

35             f[s1] = g;

36             p[g].id = g;

37             if(strcmp(s1,"Isenbaev")==0)

38             u = g;

39             strcpy(p[g].s,s1);

40         }

41         if(!f[s2])

42         {

43             g++;

44             f[s2] = g;

45             p[g].id = g;

46             if(strcmp(s2,"Isenbaev")==0)

47             u = g;

48             strcpy(p[g].s,s2);

49         }

50         if(!f[s3])

51         {

52             g++;

53             f[s3] = g;

54             p[g].id = g;

55             if(strcmp(s3,"Isenbaev")==0)

56             u = g;

57             strcpy(p[g].s,s3);

58         }

59         w[f[s1]][f[s2]] = 1;

60         w[f[s2]][f[s1]] = 1;

61         w[f[s1]][f[s3]] = 1;

62         w[f[s3]][f[s1]] = 1;

63         w[f[s2]][f[s3]] = 1;

64         w[f[s3]][f[s2]] = 1;

65     }

66     for(i = 1; i <= g ; i++)

67     w[i][i] = 0;

68     for(i =1 ;i <= g ; i++)

69         for(j = 1; j <= g ; j++)

70             for(k = 1; k <= g ; k++)

71             if(w[j][k]>w[j][i]+w[i][k])

72             w[j][k] = w[j][i]+w[i][k];

73     sort(p+1,p+g+1,cmp);

74     for(i = 1; i <= g ; i++)

75     {

76         if(u&&w[p[i].id][u]!=INF)

77         printf("%s %d\n",p[i].s,w[p[i].id][u]);

78         else

79         printf("%s undefined\n",p[i].s);

80     }

81     return 0;

82 }
View Code

 

你可能感兴趣的:(number)