URAL 1416 Confidential

Description

Zaphod Beeblebrox — President of the Imperial Galactic Government. And by chance he is an owner of enterprises that trade in secondhand pens. This is a complicated highly protable and highly competitive business. If you want to stay a leader you are to minimize your expenses all the time. And the presedent's high post helps in those aairs. But he is to keep this business in secret. As a president Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. Of course, this information is very useful to his company. Zaphod is to choose the minimal possible set of trans-planet passages so that he could pass from any planet to any other one via those passages and their total cost would be minimal. The task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. Thus, Zaphod decided to find not the cheapest passages set but the next one. As a real businessman he wants to estimate the value of his conspiracy expenses.

Input

The first input line contains two integers: N (2 ≤ N ≤ 500) is a number of planets in the Galaxy and M is an amount of possible transitions. The next M lines contain three integers ai, bi the numbers of the planets that are connected with some passage (1 ≤ ai, biN), and wi (0 ≤ wi ≤ 1000) is the transition cost. If an A to B transition is possible then a B to A transition is possible, too. The cost of those transitions are equal. There is not more than one passage between any two planets. One can reach any planet from any other planet via some chain of these passages.

Output

You should find two different sets of transitions with the minimal possible cost and output theirs costs. Print the minimal possible cost first. If any of those sets of transitions does not exist denote it's cost by −1.

Sample Input

inputoutput
4 6
1 2 2
2 3 2
3 4 2
4 1 2
1 3 1
2 4 1
Cost: 4
Cost: 4
3 2
1 2 2
2 3 2
Cost: 4
Cost: -1
解析

次小生成树 在prim的基础上多维护一个f[i][j]表示i到j最长的边。次小生成树就是在最小生成树的基础上去掉一条边,在加上另一条稍长的边。

http://www.cppblog.com/MatoNo1/archive/2011/05/29/147627.aspx讲的很详细

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

using namespace std;

#define INF 0x3f3f3f3f

int N,M,Map[510][510],Pre[510],Dist[510],f[510][510];

void readdata()
{
	memset(Map,0x3f,sizeof(Map));
	//memset(f,0x3f,sizeof(f));

	scanf("%d%d",&N,&M);

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

void prim()
{
	Dist[1]=-1;
	for(int i=2;i<=N;i++) {Pre[i]=1;Dist[i]=Map[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;
		Map[k][Pre[k]]=Map[Pre[k]][k]=INF;
		for(int j=1;j<=N;j++)
			if(Dist[j]==-1) f[j][k]=f[k][j]=max(min,f[Pre[k]][j]);
		Dist[k]=-1;
		sum+=min;

		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("Cost: %d\n",sum);
	
	int min=INF;
	bool flag=0;
	for(int i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			if(Map[i][j]!=INF && min>Map[i][j]-f[i][j]) {min=Map[i][j]-f[i][j]; flag=1;}
	if(!flag) printf("Cost: -1");
	else printf("Cost: %d",sum+min);	
}

int main()
{
	freopen("URAL1416Confidential.in","r",stdin);

	readdata();
	prim();

	while(1);
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值