首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2377 - Bad Cowtractors(最大生成树)

【POJ】2377 - Bad Cowtractors(最大生成树)

作者头像
FishWang
发布2025-08-26 19:59:17
发布2025-08-26 19:59:17
18200
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Bad Cowtractors

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 13493

Accepted: 5581

Description

Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie. Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".

Input

* Line 1: Two space-separated integers: N and M * Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.

Output

* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
42

Hint

OUTPUT DETAILS: The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.

Source

USACO 2004 December Silver

最小生成树反过来就行了。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
struct node
{
	int st,endd;
	int cost;
}data[20011];
bool cmp(node a,node b)
{
	return a.cost > b.cost;
}
int f[1011];
int find(int x)
{
	if (f[x] != x)
		f[x] = find(f[x]);
	return f[x];
}
int join(int x,int y)
{
	int fx,fy;
	fx = find(x);
	fy = find(y);
	if (fx != fy)
	{
		f[fx] = fy;
		return 1;
	}
	return 0;
}
int main()
{
	int n,m;
	int flag;
	while (~scanf ("%d %d",&n,&m))
	{
		for (int i = 1 ; i <= m ; i++)
			scanf ("%d %d %d",&data[i].st,&data[i].endd,&data[i].cost);
		for (int i = 1 ; i <= n ; i++)
			f[i] = i;
		sort(data+1 , data+1+m , cmp);
		int ans = 0;
		flag = 0;
		for (int i = 1 ; i <= m ; i++)
		{
			if (join(data[i].st,data[i].endd))
				ans += data[i].cost , flag++;
			if (flag == n-1)
				break;
		}
		flag = 0;
		for (int i = 1 ; i <= n ; i++)
		{
			if (f[i] == i)
				flag++;
			if (flag >= 2)
				break;
		}
		if (flag >= 2)
			printf ("-1\n");
		else
			printf ("%d\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档