题目:
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.
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头牛去拍M部电影,每部电影都有相应的牛当演员。
同一部电影的演员两两有关系,不同的电影中的演员没有关系(关系的权值为1)。
问题是对于每头牛与其他牛的关系之和求出最小的并且*100再除以N-1.
此题由于N的值不是很大,因此可以用Floyd-Warshall算法解决,将任意2头牛之间的关系求出,然后求出每头牛与其他牛的关系总和,最后比较即可,然后进行相应的处理。
参考代码:
#include <iostream>
#include <algorithm>
#define N 320
#define INF 99999
using namespace std;
int d[N][N];
void cl(int n) {
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= n;++j) {
d[i][j] = INF;
}
}
for (int i = 1;i <= n;++i) {
d[i][i] = 0;
}
}
void floyed(int n) {
for (int k = 1;k <= n;++k) {
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= n;++j) {
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
}
}
}
int minways(int n) {
int minw = 99999;
int w;
for (int i = 1;i <= n;++i) {
w = 0;
for (int j = 1;j <= n;++j) {
if (i != j && d[i][j] != INF) w = w + d[i][j];
//cout << w << endl;
}
//cout << w << endl;
if (w < minw) minw = w;
}
return minw;
}
int average(int minw,int n) {
int aver;
aver = minw * 100 / (n-1);
return aver;
}
int main() {
int n,m;
while (cin >> n >> m) {
cl(n);
int num1;
int cows[N];
for (int i = 1;i <= m;++i) {
cin >> num1;
int k = 1;
for (int j = 1;j <= num1;++j) {
cin >> cows[k];
k++;
}
for (int i1 = 1;i1 < k;++i1) {
for (int i2 = 1;i2 < k;++i2) {
if (cows[i1] != cows[i2]) {
d[cows[i1]][cows[i2]] = 1;
}
}
}
}
floyed(n);
int minw = minways(n);
//cout << minw << endl;
int aver = average(minw,n);
cout << aver << endl;
}
return 0;
}
此题也可以用其他方法求出结果,这里就不再列出了。
话说poj里的牛真屌,又开party又拍电影,还办学校,甚至探索虫洞的奥秘,比我们还厉害。这年头,不好好学习,连牛都要鄙视你。