您的位置:首页 > 其它

POJ 2139——Six Degrees of Cowvin Bacon 最短路径

2016-03-20 21:40 525 查看
原题如下:

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 .] 

题目大意:一群牛拍电影。一起工作的牛的距离为一,不是一起工作但是同时和另一头牛工作的两头牛距离为2。求与其他牛平均距离最短的牛。

分析:很明显的是最短路径的题目。难度在于图的构建。

有几点,一个是要注意,因为要求任意两点之间的距离,弗洛伊德最好。

我把邻接矩阵和领接表都用上了,为了要遍历两头牛同时和另一头牛一起工作的情况。

具体的可以看代码注释。

代码如下:

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <ctype.h>
#include <vector>
using namespace std;
const int MAXN=310;
const int INF=1000000;
int n,m;
int d[MAXN][MAXN];//邻接矩阵
vector <int> G[MAXN];//邻接表
int a[MAXN]; //保存在同一个组工作的牛
void floyd()
{
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 main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j)
d[i][j]=0;
else
d[i][j]=INF;
}
int t;
while(m--)
{
scanf("%d",&t);
for(int i=1;i<=t;i++)
scanf("%d",a+i);
for(int i=1;i<t;i++)
{
for(int j=i+1;j<=t;j++)
{
if(i==j)
continue;
d[a[i]][a[j]]=d[a[j]][a[i]]=1;
//通过对邻接表的遍历判断距离为2的牛
for(int k=0;k<G[a[j]].size();k++)
{
if(d[a[i]][G[a[j]][k]]==INF)
d[a[i]][G[a[j]][k]]=2;
}
for(int k=0;k<G[a[i]].size();k++)
{
if(d[G[a[i]][k]][a[j]]==INF)
d[G[a[i]][k]][a[j]]=2;
}
G[a[i]].push_back(a[j]);
G[a[j]].push_back(a[i]);
}
}
}
floyd();
double minnum=INF;
double sum;
for(int i=1;i<n;i++)
{
sum=0;
for(int j=1;j<=n;j++)
{
sum+=d[i][j];
}
minnum=min(sum,minnum);
}
printf("%d\n",(int)minnum*100/(n-1));//注意是除以n-1
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: