您的位置:首页 > 其它

POJ2139 解题报告

2017-07-21 11:20 323 查看
Six Degrees of Cowvin Bacon

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 5769Accepted: 2714
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 .]

Source
USACO 2003 March Orange



这是一个简单的求 任意两点之间最短路 的问题
使用Floyd算法可以很快地解决
不过这一题由于一开始没有理解题意所以wa了
这一题的大意是,求解出一头离其他的牛距离之和最小的牛,然后算出这个距离之和的平均值再乘上100,得出来的就是答案

#include<iostream>
#include<algorithm>
using namespace std;
const int INF=1000;
const int maxn=300+30;
const int maxm=10000+100;
int dp[maxn][maxn];
int cows[maxn];
int ncow;
int N,M;
void set_cows();
void floyd();
void solve();
void show();
int main()
{
while(cin>>N>>M)
{
fill(dp[0],dp[0]+maxn*maxn,INF);
for(int i=0;i<M;i++)
{
cin>>ncow;
for(int i=0;i<ncow;i++)
cin>>cows[i];
set_cows();
}
floyd();
//show();
solve();
}
return 0;
}

void set_cows()
{
for(int i=0;i<ncow;i++)
for(int j=0;j<ncow;j++)
if(i!=j) dp[cows[i]][cows[j]]=dp[cows[j]][cows[i]]=1;
else dp[cows[i]][cows[j]]=dp[cows[j]][cows[i]]=0;
}

void floyd()
{
for(int k=1;k<=N;k++)
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
}

void solve()
{
int sum,min_sum=INF;
for(int i=1;i<=N;i++)
{
sum=0;
for(int j=1;j<=N;j++)
sum+=dp[i][j];
min_sum=min(sum,min_sum);
}
cout<<min_sum*100/(N-1)<<endl;
}

void show()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
if(dp[i][j]<INF) cout<<dp[i][j]<<"    ";
else cout<<"inf   ";
cout<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: