首页 > 代码库 > LightOJ 1029-Civil and Evil Engineer(最小/大生成树)

LightOJ 1029-Civil and Evil Engineer(最小/大生成树)

Description

A Civil Engineer is given a task to connect n houses with the main electric power station directly or indirectly. The Govt has given him permission to connect exactly n wires to connect all of them. Each of the wires connects either two houses, or a house and the power station. The costs for connecting each of the wires are given.

Since the Civil Engineer is clever enough and tries to make some profit, he made a plan. His plan is to find the best possible connection scheme and the worst possible connection scheme. Then he will report the average of the costs.

Now you are given the task to check whether the Civil Engineer is evil or not. That‘s why you want to calculate the average before he reports to the Govt.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of houses. You can assume that the houses are numbered from 1 to n and the power station is numbered 0. Each of the next lines will contain three integers in the form u v w (0 ≤ u, v ≤ n, 0 < w ≤ 10000, u ≠ v) meaning that you can connect u and v with a wire and the cost will be w. A line containing three zeroes denotes the end of the case. You may safely assume that the data is given such that it will always be possible to connect all of them. You may also assume that there will not be more than 12000 lines for a case.

Output

For each case, print the case number and the average as described. If the average is not an integer then print it inp/q form. Where p is the numerator of the result and q is the denominator of the result; p and q are relatively-prime. Otherwise print the integer average.

Sample Input

3

 

1

0 1 10

0 1 20

0 0 0

 

3

0 1 99

0 2 10

1 2 30

2 3 30

0 0 0

 

2

0 1 10

0 2 5

0 0 0

Sample Output

Case 1: 15

Case 2: 229/2

Case 3: 15

kruskal水过,题意;给一些连通的路,求最大生成树和最小生成树的和的一半,若是整数,直接输入,否则以分数模式输出。一开始求的时候大小两棵生成树用的同一个并查集,无限wa。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define L long long
using namespace std;
const int INF=1<<27;
const int maxn=200010;
int n,v[maxn],u[maxn],w[maxn],fa[maxn],eg[maxn],fa1[maxn];
bool cmp(int x,int y)
{
	return w[x]<w[y];
}
bool cmp1(int x,int y)
{
	return w[x]>w[y];
}
void Make_set()
{
	for(int i=0;i<=n;i++)
		fa[i]=i;
}
void Make_set1()
{
	for(int i=0;i<=n;i++)
		fa1[i]=i;
}
int Find(int x)
{
	return x==fa[x]?x:fa[x]=Find(fa[x]);
}
int Find1(int x)
{
	return x==fa1[x]?x:fa1[x]=Find1(fa1[x]);
}
int Kruskal(int m)
{
	int i,ans=0;
	Make_set();
	for(i=0;i<m;i++)
	  eg[i]=i;
	sort(eg,eg+m,cmp);
	for(i=0;i<m;i++)
	{
		int e=eg[i];
		int fx=Find(u[e]);
		int fy=Find(v[e]);
		if(fx!=fy)
		{
			fa[fx]=fy;
			ans+=w[e];
		}
	}
		return ans;
}
int Big_Kruskal(int m)
{
	int i,ans=0;
	Make_set1();
	for(i=0;i<m;i++)
	  eg[i]=i;
	sort(eg,eg+m,cmp1);
	for(i=0;i<m;i++)
	{
		int e=eg[i];
		int fx=Find1(u[e]);
		int fy=Find1(v[e]);
		if(fx!=fy)
		{
			fa1[fx]=fy;
			ans+=w[e];
		}
	}
		return ans;
}
int main()
{
	int T;
	scanf("%d",&T);
	for(int cas=1;cas<=T;cas++)
	{
		scanf("%d",&n);
		int i=0;
		while(cin>>u[i]>>v[i]>>w[i])
		{
			if(!u[i]&&!v[i]&&!w[i])
				break;
			i++;
		}
		//cout<<i<<endl;
		int a=Kruskal(i),b=Big_Kruskal(i);
		printf("Case %d: ",cas);
		if((a+b)%2==0)
			printf("%d\n",(a+b)/2);
		else
			printf("%d/2\n",a+b);
	}
	return 0;
}


LightOJ 1029-Civil and Evil Engineer(最小/大生成树)