URAl 1416 Confidential【次小生成树】

1416. Confidential

Time limit: 2.0 second
Memory limit: 64 MB
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 line contains integers  n and  m that are a number of planets in the Galaxy and an amount of passages between them (2 ≤  n ≤ 500). The next  m lines contain integers  aibi and  wi that are the numbers of the planets connected with the passage and the transition cost (1 ≤  aibi ≤  n; 0 ≤  wi≤ 1000). If an  A to  B transition is possible then a  B to  A transition is possible too, and the cost of these transitions are equal. There is no 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.

Samples

input output
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
Problem Author: Den Raskovalov
Problem Source: The Ural State University Championship, October 29, 2005

题意:

n个顶点,m 条边,给出每条边连接的顶点编号,以及边的权值,求使得所有点相连通需要的边的最小的权值和以及第二小的权值和是多少,不存在的话,输出-1


题解:

求最小生成树和次小生成树(非严格次小,也就是权值可以和最小的相等)是否存在,存在的话,输出他们的权值

模板,点这里,稍微修改下模板,就能AC了....


/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=505;
int map[maxn][maxn];
bool vis[maxn];
int low[maxn];
int mst[maxn][maxn],pre[maxn];
bool inmst[maxn][maxn];
int n,m;
void init()
{
	memset(map,inf,sizeof(map));
	for(int i=1;i<=n;++i)
	{
		map[i][i]=0;
	}
}
int mintree()
{
	int mincost=0;
	int next,Min;
	memset(inmst,0,sizeof(inmst));
	memset(mst,0,sizeof(mst));
	for(int i=1;i<=n;++i)
	{
		low[i]=map[1][i];
		vis[i]=0;
		pre[i]=1;
	}
	vis[1]=1;
	for(int i=2;i<=n;++i)
	{
		Min=inf;next=-1;
		for(int j=1;j<=n;++j)
		{
			if(!vis[j]&&Min>low[j])
			{
				next=j;
				Min=low[j];
			}
		}
		mincost+=Min;
		if(next==-1)
		{
			return -1;
		}
		vis[next]=1;
		int fa=pre[next];
		inmst[next][fa]=inmst[fa][next]=1;
		for(int j=1;j<=n;++j)
		{
			if(vis[j]&&j!=next)
			{
				mst[j][next]=mst[next][j]=max(mst[fa][j],low[next]);
			}
			if(!vis[j]&&low[j]>map[next][j])
			{
				low[j]=map[next][j];
				pre[j]=next;
			}
		}
	}
	return mincost;
}
int sectree(int x)//参数是最小生成树的权值 
{
	int ans=inf;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<i;++j)
		{
			if(map[i][j]!=inf&&!inmst[i][j])
			{
				ans=min(ans,x-mst[i][j]+map[i][j]);
			}
		}
	}
	return ans==inf?-1:ans;//不存在的为-1,否则返回权值 
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init();
		for(int i=1;i<=m;++i)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			map[a][b]=map[b][a]=min(map[a][b],c);
		}
		int cost1=mintree(),cost2=cost1==-1?-1:sectree(cost1);
		printf("Cost: %d\n",cost1);
		printf("Cost: %d\n",cost2);
	}
	return 0;
}


你可能感兴趣的:(URAl 1416 Confidential【次小生成树】)