本题链接:http://poj.org/problem?id=2139
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
* 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 .]
题意:有一群牛在拍电影(牛们的生活真丰富),如果两头牛在同一部电影中出现过,那么这两头牛的度就为1,如果a与b之间有n头媒介牛,那么a,b的度为n+1。 给出m部电影,每一部给出牛的个数和编号。问哪一头到其他每头牛的度数平均值最小,输出 最小 平均值 乘100。
解题思路:只要把任意两个牛之间的最小度求出来然后就处理就行了,可以考虑用Floyd算法(三个for)。
参考代码:
1 #include <cstring> 2 #include <iostream> 3 #define INF 9999999 4 #define maxn 300 5 using namespace std; 6 7 int cost[maxn][maxn]; 8 int p[maxn]; 9 int V; 10 11 void fl () { 12 for (int k = 1; k <= V; ++k) { 13 for (int i = 1; i <= V; ++i) { 14 for (int j = 1; j <= V; ++j) { 15 cost[i][j] = min (cost[i][j], cost[i][k] + cost[k][j]); 16 } 17 } 18 } 19 } 20 21 int main () { 22 int x, y; 23 int a, b; 24 int n; 25 26 cin >> V >> n; 27 28 //-----------------------------------------初始化 29 memset (p, 0, sizeof(p)); 30 for (int i = 1;i <= V; ++i) 31 for (int j = 1; j <= V; ++j) 32 cost[i][j] = INF; 33 for (int i = 1; i <= V; ++i) 34 cost[i][i] = 0; 35 //-----------------------------------------整理输入数据 36 37 while (n--) { 38 cin >> y; 39 for (int i = 1; i <= y; ++i) { 40 cin >> p[i]; 41 } 42 for (int i = 1;i <= y; ++i) { 43 for (int j = i + 1; j <= y; ++j) { 44 a = p[i]; 45 b = p[j]; 46 cost[b][a] = cost[a][b] = 1;///a和b之间的距离 47 } 48 } 49 } 50 51 fl ();//--------------调用函数 52 53 //-------------------------------------------------------求最小值输出 54 int sum, minsum = INF; 55 int i, j; 56 57 for (i = 1; i <= V; ++i) { 58 sum = 0; 59 for (j = 1; j <= V; ++j) { 60 sum += cost[i][j]; 61 } 62 if (sum < minsum)//求最小值 63 minsum = sum; 64 } 65 66 cout << (minsum * 100) / (V - 1) << endl; //输出 最小 平均值 67 68 return 0; 69 }
欢迎码友评论,一起成长。