POJ - 1679 The Unique MST(次小生成树)

The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35193   Accepted: 12860

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique. 

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
1. V' = V. 
2. T is connected and acyclic. 

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

Source

POJ Monthly--2004.06.27 srbga@POJ

 

          大体意思就是建一个生成树,让其中一个边消耗为0后,求(边两边的城市人口数量总和)/ 生成树总消耗       的最大值;

          这样的话,我们就可以先建出最小生成树,然后枚举每一个边,假如这条边在最小生成树中用到了,那么生成树的最小消耗就是mst-这条边的消耗,如果没有被用到,那么和最小生成树放在一起之后就会形成一个环,那么最小消耗就是mst-这个环里的最长边;

 

         

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
double dis[1005];
double Max[1005][1005];
int pre[1005];
double Map[1005][1005];
bool use[1005][1005];
bool vis[1005];
int v[1005];
struct ***
{
	int x, y;
}loca[1005];
int n, m;
double cal(int x, int y)
{
	double len= sqrt((loca[x].x - loca[y].x)*(loca[x].x - loca[y].x) + (loca[x].y - loca[y].y)*(loca[x].y - loca[y].y));
	return len;
}
double prim()
{
	double ans = 0;
	memset(use, 0, sizeof(use));
	memset(vis, 0, sizeof(vis));
	memset(Max, 0, sizeof(Max));
	for (int s = 2; s <= n; s++)
	{
		dis[s] = Map[1][s];
		pre[s] = 1;
	}
	vis[1] = 1;
	pre[1] = 0;
	dis[1] = 0;
	for (int s = 2; s <= n; s++)
	{
		double min_ans = inf*1.0;
		int k;
		for (int s = 1; s <= n; s++)
		{
			if (!vis[s] && min_ans > dis[s])
			{
				min_ans = dis[s];
				k = s;
			}
		}
		if (min_ans == inf)return -1;
		ans += min_ans;
		vis[k] = 1;
		use[k][pre[k]] = use[pre[k]][k] = 1;
		for (int s = 1; s <= n; s++)
		{
			if (vis[s] && s != k)
				Max[s][k] = Max[k][s] = max(Max[s][pre[k]], dis[k]);
			if (!vis[s]&&dis[s]>Map[k][s])
			{
				dis[s] = Map[k][s];
				pre[s] = k;
			}
		}
	}
	return ans;
}
int main()
{
	int te;
	scanf("%d", &te);
	while (te--)
	{
		scanf("%d", &n);
		for (int s = 1; s <= n; s++)
			scanf("%d%d%d", &loca[s].x, &loca[s].y, &v[s]);
		for (int s = 1; s < n; s++)
			for (int w = s + 1; w <= n; w++)
				Map[s][w] = Map[w][s] = cal(s, w);
		double mst = prim();
		double ans = 0;
		for (int s = 1; s < n; s++)
		{
			for (int w = s + 1; w <= n; w++)
			{
			
				if (use[s][w])
				{
					ans = max(ans, (v[s] + v[w])*1.0 / (mst - Map[s][w]));
				}
				else
				{
					ans = max(ans, (v[s] + v[w])*1.0 / (mst - Max[s][w]));
				}
			}
		}
		printf("%.2lf\n", ans);
	}
}

 

全部评论

相关推荐

秋招进行到现在终于能写总结了。完全没想到战线会拉这么长,过程会如此狼狈,不过更应该怪自己太菜了。好在所有的运气都用在了最后,也是有个去处。背景:双2本硕科班,无竞赛,本科一段研究所实习,硕士一段大厂暑期实习但无转正。技术栈是C++&nbsp;&amp;&nbsp;Golang,实习是客户端音视频(而且是鸿蒙端开发),简历两个C++项目一个Golang项目。主要投递岗位:后端,cpp软开,游戏服务端,测开,以及一些不拘泥于Java的岗位。从8月起总共投递123家公司,笔试数不清了,约面大约30家。offer/oc/意向:友塔游戏(第一个offer,面试体验很好,就是给钱好少南瑞继保(计算机科班点击就送(限男生),不...
乡土丁真真:佬很厉害,羡慕~虽然我还没有到校招的时候,也想讲一下自己的看法:我觉得不是CPP的问题,佬的背书双2,技术栈加了GO,有两段实习。投了123,面了30.拿到11个offer。这个数据已经很耀眼了。这不也是CPP带来的吗?当然也不止是CPP。至少来说在这个方向努力过的也会有好的结果和选择。同等学历和项目选java就会有更好的吗?我个人持疑问态度。当然CPP在方向选择上确实让人头大,但是我觉得能上岸,至于最后做什么方向,在我看来并不重要。至于CPP特殊,有岗位方向的随机性,java不是不挑方向,只是没得选而已。也希望自己以后校招的时候能offer满满
点赞 评论 收藏
分享
牛客279957775号:铁暗恋
点赞 评论 收藏
分享
11-27 12:43
已编辑
门头沟学院 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务