POJ 2139 Six Degrees of Cowvin Bacon (floyd)

Six Degrees of Cowvin Bacon

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
Problem Description
The cows have been making movies lately, so they are ready to play a variant of the famous game "Six Degrees of Kevin Bacon".

The game works like this: each cow is considered to be zero degrees of separation (degrees) away from herself. If two distinct cows have been in a movie together, each is considered to be one 'degree' away from the other. If a two cows have never worked together but have both worked with a third cow, they are considered to be two 'degrees' away from each other (counted as: one degree to the cow they've worked with and one more to the other cow). This scales to the general case.

The N (2 <= N <= 300) cows are interested in figuring out which cow has the smallest average degree of separation from all the other cows. excluding herself of course. The cows have made M (1 <= M <= 10000) movies and it is guaranteed that some relationship path exists between every pair of cows.
 
Input
* Line 1: Two space-separated integers: N and M <br> <br>* Lines 2..M+1: Each input line contains a set of two or more space-separated integers that describes the cows appearing in a single movie. The first integer is the number of cows participating in the described movie, (e.g., Mi); the subsequent Mi integers tell which cows were. <br>
 
Output
* Line 1: A single integer that is 100 times the shortest mean degree of separation of any of the cows. <br>
 
Sample Input
4 2 3 1 2 3 2 3 4
 
Sample Output
100
 
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <string>
 5 #include <cstdio>
 6 #define inf 0x3f3f3f3f
 7 using namespace std;
 8 int e[305][305];
 9 int n, m;
10 int main()
11 {
12     int n, m;
13     cin >> n >> m;
14     int i, j;
15     int a[305];
16     for (i = 1; i <= n; i++)
17     {
18         for (j = 1; j <= n; j++)
19         {
20             if (i == j) e[i][j] = 0;
21             else e[i][j] = inf;
22         }
23     }
24     for (i = 1; i <= m; i++)
25     {
26         int k,p;
27         cin >> k;
28         for (j = 1; j <= k; j++)
29         {
30             cin >> a[j];
31         }
32         for (j = 1; j <= k; j++)
33         {
34             for (p = j+1; p <= k; p++)
35             {
36                 e[a[j]][a[p]] = 1;
37                 e[a[p]][a[j]] = 1;
38             }
39         }
40     }
41     int k;
42     for (k = 1; k <= n; k++)
43     {
44         for (i = 1; i <= n; i++)
45         {
46             for (j = 1; j <= n; j++)
47             {
48                 if (e[i][j] > e[i][k] + e[k][j])
49                     e[i][j] = e[i][k] + e[k][j];
50             }
51         }
52     }
53     int ans = inf;
54     int sum = 0;
55     for (i = 1; i <= n; i++)
56     {
57         sum = 0;
58         for (j = 1; j <= n; j++)
59         {
60             if (e[i][j] != inf)
61             {
62                 sum += e[i][j];
63             }
64         }
65         if (sum < ans) ans = sum;
66     }
67     ans= ans*100/ (n-1) ;
68     cout <<ans<< endl;
69 }
原文地址:https://www.cnblogs.com/caiyishuai/p/8439537.html