Six Degrees of Cowvin Bacon
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2429 | Accepted: 1145 |
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 .]
题意:
给出 N (2 ~ 300),M (1 ~ 10000)。代表有 N 头牛,M 部电影,同一部电影里面的牛之间的距离为 1。后给出 M 部电影,每部电影第一数字代表这部电影牛的个数 K,后给出这 K 头牛是第几号牛。计算每头牛到其他牛之间最短路的总和的平均值,输出这个最小的平均值 * 100的值。
思路:
最短路。Floyd 计算每对牛之间的最短路,后循环求出总和且比较大小。得出最小值后要除以(n - 1),注意要开浮点型来除,得出的数乘以100后转化为整型输出即可。题意较难懂,说得不是太明白,从已知条件得出某些牛之间的距离后可以算出不在同一部电影牛之间的最短路。
AC:
#include <cstdio>
#include <queue>
#define INF 999999
using namespace std;
int w[305][305],cow[305];
int main() {
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
w[i][j] = INF;
while(m--) {
int ans;
scanf("%d",&ans);
for(int i = 1;i <= ans;i++)
scanf("%d",&cow[i]);
for(int i = 1;i <= ans;i++)
for(int j = i + 1;j <= ans;j++) {
w[cow[i]][cow[j]] = 1;
w[cow[j]][cow[i]] = 1;
}
}
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++) {
if( w[i][k] < INF &&
w[k][j] < INF &&
w[i][j] > w[i][k] + w[k][j] ) {
w[i][j] = w[i][k] + w[k][j];
}
}
float min_ave = INF;
for(int i = 1;i <= n;i++) {
int sum = 0;
for(int j = 1;j <= n;j++) {
if(w[i][j] < INF && i != j)
sum += w[i][j];
}
if(sum < min_ave) min_ave = sum;
}
min_ave /= ((n - 1) * 1.0);
printf("%d\n",(int)(min_ave * 100));
return 0;
}