POJ-2139 Six Degrees of Cowvin Bacon---Floyd

题目链接:

https://vjudge.net/problem/POJ-2139

题目大意:

给定一些牛的关系,他们之间的距离为1。

然后求当前这只牛到每只牛的最短路的和,除以 n - 1只牛的最大值。(这里直接取整就行啦)

思路:

floyd来求最短路。

然后枚举求max就行了。

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<set>
 8 #include<map>
 9 #include<cmath>
10 using namespace std;
11 typedef pair<int, int> Pair;
12 typedef long long ll;
13 const int INF = 0x3f3f3f3f;
14 int T, n, m;
15 const int maxn = 2e4 + 10;
16 int Map[305][305];
17 int x[305];
18 ll sum[305];
19 void floyd()
20 {
21     for(int k = 1; k <= n; k++)
22     {
23         for(int i = 1; i <= n; i++)
24         {
25             for(int j = 1; j <= n; j++)
26             {
27                 Map[i][j] = min(Map[i][j], Map[i][k] + Map[k][j]);
28             }
29         }
30     }
31     ll sum, minn = INF;
32     for(int i = 1; i <= n; i++)
33     {
34         sum = 0;
35         for(int j = 1; j <= n; j++)
36             sum += (ll)Map[i][j];
37         minn = min(minn, sum);
38     }
39     int ans = minn * 100 / (n - 1);
40     cout<<ans<<endl;
41 }
42 int main()
43 {
44     cin >> n >> m;
45     memset(Map, INF, sizeof(Map));
46     for(int i = 1; i <= n; i++)Map[i][i] = 0;
47     while(m--)
48     {
49         int t;
50         cin >> t;
51         for(int i = 0; i < t; i++)cin >> x[i];
52         for(int i = 0; i < t; i++)
53             for(int j = i + 1; j < t; j++)
54             Map[x[i]][x[j]] = Map[x[j]][x[i]] = 1;
55 
56     }
57     floyd();
58 }

 

posted @ 2018-04-13 11:31  _努力努力再努力x  阅读(160)  评论(0编辑  收藏  举报