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 .]题意是要找出最大的花费,并且不能有环,可以用生成最小树的方法,只要在排序时按从大到小排序,先选则最大的路径。至于怎样判断所有点都连通,可以用ans来对连通的点计数,如果ans=n,说明每个点都互相连通。
#include<stdio.h>
#include<iostream>#include<algorithm>
using namespace std;
#define N 1005
int n,m,ans,pre[N],sum;
struct node
{
int x,y,cost;
}arr[40010];
bool cmp(node a,node b)
{
return a.cost>b.cost;
}
int find(int x)
{
while(x!=pre[x])
x=pre[x];
return x;
}
int kruskal()
{
for(int i=1;i<=n;i++)
pre[i]=i;//一定要初始化,之前没有初始化,程序运行错误。
sort(arr+1,arr+2*m,cmp);
int a,b;
for(int i=1;i<=2*m;i++)
{
a=find(arr[i].x);
b=find(arr[i].y);
if(a!=b)
{
sum+=arr[i].cost;
ans=ans+1;
pre[b]=a;
}
}
if(ans<n)
return -1;
if(ans==n)
return sum;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int a,b,c,i;
for(i=1;i<=2*m;i++)
{
scanf("%d%d%d",&a,&b,&c);
arr[i].x=a;
arr[i].y=b;
arr[i].cost=c;
i++;
arr[i].x=a;
arr[i].y=b;
arr[i].cost=c;
}
ans=1;
sum=0;
int k;
k=kruskal();
if(k)
printf("%d\n",k);
else
printf("-1");
}
return 0;
}