ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-11 10:05:41  阅读:320  来源: 互联网

标签:case given Civil LightOJ 生成 will int edge connect


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;
} 

 

标签:case,given,Civil,LightOJ,生成,will,int,edge,connect
来源: https://blog.csdn.net/qq_41505957/article/details/99167815

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有