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 .]
题意其实挺难理解的,真的。意思就是每部电影会有几个牛合作演出,那么这几个牛之间的距离就是1,这个题的第一个难点就是建图,说难,其实就是要想办法把几个点存下来,反正都是题意惹的祸= =。最后求得就是每一个牛的所有最短路的平均值的100倍,注意是除以n-1个,而不是n个的平均值。最后要记得班结果准换成整数。
AC代码:
/
// // //
// Created by Team 3 //
// Copyright (c) 2015年 Team 3. All rights reserved. //
/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define cir(i,a,b) for (int i=a;i<=b;i++)
#define CIR(j,a,b) for (int j=a;j>=b;j--)
#define CLR(x) memset(x,0,sizeof(x))
typedef long long ll;
using namespace std;
int N,M,m=0;
#define maxn 400
int d[maxn][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()
{
while (cin >> N >> M)
{
for (int i=1;i<=maxn;i++)
for (int j=1;j<=maxn;j++)
d[i][j]=i==j?0:INF;
for (int i=1;i<=M;i++)
{
int n;
cin >> n;
for (int i=1;i<=n;i++)
cin >>a[i];
for (int k=1;k<=n;k++)
for (int l=1;l<=n;l++)
{
if(a[k]==a[l]) d[a[k]][a[l]]=0;
else d[a[k]][a[l]]=1;
}
}
floyd();
int ans=INF;
for (int i=1;i<=N;i++)
{
int temp=0;
for (int j=1;j<=N;j++)
temp+=d[i][j];
ans=min(ans,temp);
}
// cout << ans << endl;
double tp=ans*1.0/(N-1);
ans=tp*100;
cout << ans << endl;
}
return 0;
}