题目:POJ2139
Six Degrees of Cowvin Bacon
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6031 | Accepted: 2831 |
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
题目大意:
n头奶牛,m部电影,每部电影告诉你是哪几头奶牛.如果两头奶牛在一起出演就相互度=1,如果两头奶牛没有一起出演过,但是同时跟第三头奶牛出演过,那么这两头奶牛的相互度就等于他们与第三头奶牛的相互度之和。要求输出牛的最小平均分开度的100倍。
用Floyd-Warshall算法
题目AC情况:
代码C语言:
# include <stdio.h>
# define N 301
# define MAX 100000
# define min(a,b) (a)<(b)?(a):(b)
void PP(int v);//Floyd-Warshall算法
int D[N][N],X[N],V,M;//D数组D[i][j]表示边 <i,j>的权值
int main(){
int i,j,n,sum;
//freopen("WE.txt","r",stdin);
scanf("%d %d",&V,&M);
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
D[i][j]=MAX;//初始化
D[i][i]=0;//定义本节点为无穷大
}
while(M--)
{
scanf("%d",&n);
for(i=0;i<n;X[i]--,i++)
scanf("%d",&X[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
D[X[i]][X[j]]=D[X[j]][X[i]]=1;
}
PP(V);
sum=MAX;
for(i=0;i<V;i++)
{
n=0;
for(j=0;j<V;j++)
n+=D[i][j];
sum=min(sum,n);
}
printf("%d\n",100*sum/(V-1));
return 0;
}
void PP(int v)
{
int i,j,k;
for(i=0;i<v;i++)
for(j=0;j<v;j++)
for(k=0;k<v;k++)
D[j][k]=min(D[j][k],D[j][i]+D[i][k]);
}