UVA 10600 ACM Contest and Blackout

In order to prepare the �The First National ACM School Contest�(in 20??) the major of the city decided to provide all the schools with a reliable source of power. (The major is really afraid of blackoutsJ). So, in order to do that, power station �Future� and one school (doesn�t matter which one) must be connected; in addition, some schools must be connected as well.

 

You may assume that a school has a reliable source of power if it�s connected directly to �Future�, or to any other school that has a reliable source of power. You are given the cost of connection between some schools. The major has decided to pick out two the cheapest connection plans � the cost of the connection is equal to the sum of the connections between the schools. Your task is to help the major � find the cost of the two cheapest connection plans.

 

Input

The Input starts with the number of test cases, T (1�T�15) on a line. Then T test cases follow. The first line of every test case contains two numbers, which are separated by a space, N (3�N�100) the number of schools in the city, and M the number of possible connections among them. Next M lines contain three numbers Ai, Bi, Ci , where Ci� is the cost of the connection (1�Ci�300) between schools Ai� and Bi. The schools are numbered with integers in the range 1 to N.

 

Output

For every test case print only one line of output. This line should contain two numbers separated by a single space - the cost of two the cheapest connection plans. Let S1 be the cheapest cost and S2 the next cheapest cost. It�s important, that S1=S2 if and only if there are two cheapest plans, otherwise S1�S2. You can assume that it is always possible to find the costs S1 and S2..

 

Sample Input

Sample Output

2

5 8

1 3 75

3 4 51

2 4 19

3 2 95

2 5 42

5 4 31

1 2 9

3 5 66

9 14

1 2 4

1 8 8

2 8 11

3 2 8

8 9 7

8 7 1

7 9 6

9 3 2

3 4 7

3 6 4

7 6 2

4 6 14

4 5 9

5 6 10

110 121

37 37

解析

次小生成树

#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

#define INF 0x3f3f3f3f

int Map[110][110],f[110][110],N,M,Dist[110],Pre[110];
//int Fa[110],Deep[110];

void readdata()
{
	memset(Map,0x3f,sizeof(Map));
	scanf("%d%d",&N,&M);

	//for(int i=1;i<=N;i++) {Fa[i]=i;Deep[i]=1;}
	
	for(int i=1;i<=M;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		Map[a][b]=Map[b][a]=c;
	}
}
/*
int find(int x)
{
	if(Fa[x]==x) return x;
	Fa[x]=find(Fa[x]);
	return Fa[x];
}

void merge(int a,int b);
{
	int af=find(a),bf=find(b);
	if(deep[af]>deep[bf]) Fa[bf]=af;
	else if(deep[af]<deep[bf]) Fa[af]=bf;
	else {Fa[af]=bf;deep[bf]++;}
}
*/
void prim()
{
	Dist[1]=-1;
	for(int i=2;i<=N;i++) {Dist[i]=Map[i][1]; Pre[i]=1;}

	int sum=0;
	for(int i=2;i<=N;i++)
	{
		int min=INF,k=0;
		for(int j=2;j<=N;j++)
			if(Dist[j]!=-1 && min>Dist[j]) {min=Dist[j];k=j;}

		if(k==0) break;
		sum+=min;
		Map[k][Pre[k]]=Map[Pre[k]][k]=INF;
		for(int j=1;j<=N;j++) if(Dist[j]==-1) f[k][j]=f[j][k]=max(min,f[Pre[k]][j]);
		Dist[k]=-1;
	
		for(int j=2;j<=N;j++)
			if(Dist[j]!=-1 && Dist[j]>Map[j][k]) {Dist[j]=Map[j][k];Pre[j]=k;}
	}
	printf("%d ",sum);

	int min=INF;
	for(int i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
		{
			if(i==j) continue;
			if(Map[i][j]!=INF && min>Map[i][j]-f[i][j])
				min=Map[i][j]-f[i][j];
		}
	printf("%d\n",sum+min);
}

int main()
{
	freopen("ACMContestAndBlackout.in","r",stdin);
	freopen("ACMContestAndBlackout.out","w",stdout);

	int T;
	scanf("%d",&T);
	for(int i=1;i<=T;i++)
	{
		readdata();
		prim();
	}

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值