POJ2139 Six Degrees of Cowvin Bacon
Six Degrees of Cowvin Bacon
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1891 | Accepted: 886 |
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.
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
* 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.
* 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.
Output
* Line 1: A single integer that is 100 times the shortest mean degree of separation of any of the cows.
Sample Input
4 2 3 1 2 3 2 3 4
Sample Output
100
Hint
[Cow 3 has worked with all the other cows and thus has degrees of separation: 1, 1, and 1 -- a mean of 1.00 .]
Source
思路:本题题意可以变换的理解为如果N个点在一个集合中,则这些点之间的距离为1。然后由此建立一个无向图。在这N个点中,每一个点与其他的所有点都有一个连接的路径长度,将这些长度都加起来,然后除以N-1,就求出了平均长度。题目所求为这些平均长度中的最小值,然后将最小值乘以100输出。
本题可以先建立邻接矩阵,然后再使用FLOYD就可以求出了。
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 8 9 #define MAXINT 99999999 10 11 using namespace std; 12 13 int main(int argc, char *argv[]) 14 { 15 16 int n,m; 17 scanf("%d%d",&n,&m); 18 19 int data[304][304]; 20 21 int array[304]; 22 23 24 int i,j,k; 25 26 27 for(i=1;i<=n;i++) 28 for(j=1;j<=n;j++) 29 {data[i][j]=MAXINT;} 30 31 32 33 34 for(i=1;i<=m;i++) 35 { 36 int num; 37 scanf("%d",&num); 38 39 40 41 for(j=1;j<=num;j++) 42 { 43 44 scanf("%d",&array[j]); 45 46 47 48 49 } 50 51 52 for(j=1;j<=num;j++) 53 for(k=j+1;k<=num;k++) 54 data[array[j]][array[k]]=data[array[k]][array[j]]=1; 55 56 57 58 } 59 60 61 for(k=1;k<=n;k++) 62 for(i=1;i<=n;i++) 63 for(j=1;j<=n;j++) 64 { 65 if(data[i][j]>data[i][k]+data[k][j]) 66 data[i][j]=data[i][k]+data[k][j]; 67 } 68 69 70 long long sum=0; 71 72 long long minsum=MAXINT; 73 74 for(i=1;i<=n;i++) 75 {sum=0; 76 for(j=1;j<=n;j++) 77 { 78 if(i==j) 79 continue; 80 sum+=data[i][j]; 81 } 82 83 if(sum<minsum) 84 minsum=sum; 85 } 86 87 88 89 90 91 92 printf("%d\n",(minsum*100/(n-1))); 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 // system("PAUSE"); 108 return EXIT_SUCCESS; 109 }