POJ 2139 Six Degrees of Cowvin Bacon (Floyd最短路)

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

题意:

牛们最近在拍电影,所以他们准备去玩一个游戏——“六度分割”的变体。 游戏是这样进行的:每个牛离自己的距离是0度,如果两个不同的牛同时出现在一个电影里,那么他们之间的距离为1度,如果两只牛从未一起工作,但它们都与第三只牛一起工作,那么他们之间的距离为2度。 这N(2<=N<=300)头牛对找出那只牛与所有牛之间的平均距离最短感兴趣。当然,不算上他自己。这些牛拍了M(1<=M<=10000)部电影,并且保证每两个牛之间都有一定的关系。求那一头牛与其它牛距离的平均值最小值,把它乘100输出。

题解:

在一起工作的牛们相互之间距离设为1,最后求其中一头牛到其他牛距离之和的最小平均值。用Floyd求任意两点间的最短路径。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=305,INF=0x3f3f3f3f;
int d[maxn][maxn];
int a[maxn];
int n,m;
void init()
{
    for(int i=0;i<maxn;i++)
        for(int j=0;j<maxn;j++)
            if(i==j)
                d[i][j]=0;
            else
                d[i][j]=INF;
}
void warshall_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()
{
    cin>>n>>m;
    init();
    while(m--)
    {
        int k;
        cin>>k;
        for(int i=0;i<k;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<k;i++)
            for(int j=i+1;j<k;j++)
                d[a[i]][a[j]]=d[a[j]][a[i]]=1;
    }
    warshall_floyd();
    int ans=INF;
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        for(int j=1;j<=n;j++)
            sum+=d[i][j];
        ans=min(ans,sum);
    }
    cout<<ans*100/(n-1)<<endl;
    return 0;
}
posted @ 2017-11-01 17:31  Zireael  阅读(600)  评论(0编辑  收藏  举报