【Floyd】POJ1125-Stockbroker Grapevine

水题,裸的Floyd。最后要求遍历一遍图的最短路径,只需要枚举将当前每一个点作为起始点。如果它不能到达其中的某一点,则该点不可能作为起始点;否则,由该点开始遍历全图的最短路径是到所有点距离中的最大值。最终结果就是这些最大值中的最小值。

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int MAXN=100+5;
 5 const int INF=10000;
 6 int map[MAXN][MAXN];
 7 
 8 int main()
 9 {
10     int n;
11     while (scanf("%d",&n))
12     {
13         if (n==0) break;
14         for (int i=0;i<n;i++)
15             for (int j=0;j<n;j++) map[i][j]=INF;
16             
17         for (int i=0;i<n;i++)
18         {
19             int m;
20             scanf("%d",&m);
21             for (int j=0;j<m;j++)
22             {
23                 int people,time;
24                 scanf("%d%d",&people,&time);
25                 map[i][people-1]=time;
26             }
27         }
28             
29         for (int k=0;k<n;k++)
30             for (int i=0;i<n;i++)
31                 for (int j=0;j<n;j++)
32                         if (i!=j && k!=i && k!=j && map[i][j]>map[i][k]+map[k][j])
33                             {
34                                 map[i][j]=map[i][k]+map[k][j];
35                             }
36 
37         
38         int ans=INF,ansb;
39         for (int i=0;i<n;i++)
40         {
41             bool reach=true;
42             int maxlen=-100;
43             for (int j=0;j<n && reach;j++)
44             {
45                 if (i!=j && map[i][j]==INF) reach=false;
46                 if (i!=j && map[i][j]>maxlen) maxlen=map[i][j];
47             }
48             if (reach && maxlen<ans)
49             {
50                 ans=maxlen;
51                 ansb=i;
52             }
53         }
54         if (ans==INF) cout<<"disjoint";else cout<<ansb+1<<' '<<ans;
55         cout<<endl;
56     }
57 
58     return 0;
59 }
原文地址:https://www.cnblogs.com/iiyiyi/p/4698480.html