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

https://vjudge.net/problem/LightOJ-1029

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 in p/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

题意分析:

用n条线路连接n个房屋和主电站,求最好情况和最坏情况的平均值。

解题思路:

按价格从小到大排序求最小生成树, 从大到小排序求最大生成树,输出平均值。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 120
using namespace std;
struct edge{
	int u;
	int v;
	int w;
}e[N*N];
int n, m, f[N];
int cmp(edge a, edge b)
{
	return a.w>b.w;
}
int cmt(edge a, edge b)
{
	return a.w<b.w;
}
int getf(int v)
{
	if(f[v]==v)
		return v;
	f[v]=getf(f[v]);
	return f[v];
}
int merge(int u, int v)
{
	int t1, t2;
	t1=getf(u);
	t2=getf(v);
	if(t1!=t2)
	{
		f[t1]=t2;
		return 1;
	}
	return 0;
}
int Kstra()
{
	int i;
	for(i=0; i<=n; i++)
		f[i]=i;
	
	int count=0, sum=0;
	for(i=1; i<=m; i++)
	{
		if(merge(e[i].u, e[i].v))
		{
			count++;
			sum+=e[i].w;
		}
		if(count==n)
			break;
	}
	return sum;
}
int main()
{
	int T, t=0, i, j, u, v, w, sum;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		m=0;
		while(scanf("%d%d%d", &u, &v, &w))
		{
			if(u==0 && v==0 && w==0)
				break;
			e[++m].u=u;
			e[m].v=v;
			e[m].w=w;
		}
		sum=0;
		sort(e+1, e+m+1, cmp);
		sum+=Kstra();
		sort(e+1, e+m+1, cmt);
		sum+=Kstra();
		if(sum%2==1)
			printf("Case %d: %d/2\n", ++t, sum);
		else
			printf("Case %d: %d\n", ++t, sum/2);
	}
	return 0;
} 

 

上一篇:Autodesk Civil 3D 2019 Essential Training Autodesk Civil 3D 2019基本培训 Lynda课程中文字幕


下一篇:switch注意