POJ 2377 Bad Cowtractors (Kruskal)
题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-1。
思路:将边权降序再Kruskal,再检查一下是否只有一棵树即可,即根节点只有一个
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std; int N, M; // 节点,边的数量
struct edge {
int from, to, dist;
bool operator<(const edge &b) const {
return dist > b.dist;
}
} es[20006];
int par[1005];
void init() {
for (int i = 1; i <= N; ++i) par[i] = i;
}
int find(int x) {
return x == par[x] ? x : par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) par[x] = y;
}
int kruskal() {
int res = 0;
init();
sort(es + 1, es + 1 + M);
for (int i = 1; i <= M; ++i) {
edge e = es[i];
//printf("u:%d v:%d d:%d\n", e.from, e.to, e.dist);
if (find(e.from) != find(e.to)) {
unite(e.from, e.to);
res += e.dist;
}
}
return res;
}
void solve() {
int res = kruskal();
int cnt = 0;
for (int i = 1; i <= N; ++i)
if (find(i) == i) ++cnt; // 求出根节点数,如果最终结果为一棵树,那么根节点只有一个
if (cnt != 1) // 根节点数为1才能形成树
cout << -1 << endl;
else
cout << res << endl;
}
int main()
{
int u, v, d;
while (cin >> N >> M) {
for (int i=1; i <= M; ++i)
{
cin >> u >> v >> d;
es[i].from = u;
es[i].to = v;
es[i].dist = d;
}
solve();
}
return 0;
}
POJ 2377 Bad Cowtractors (Kruskal)的更多相关文章
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...
- poj 2377 Bad Cowtractors (最大生成树prim)
Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- POJ - 2377 Bad Cowtractors Kru最大生成树
Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...
- POJ 2377 Bad Cowtractors( 最小生成树转化 )
链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...
- poj 2377 Bad Cowtractors(最大生成树!)
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- MST:Bad Cowtractors(POJ 2377)
坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...
- poj 2377 拉最长的线问题 kruskal算法
题意:建光纤的时候,拉一条最长的线 思路:最大生成树 将图的n个顶点看成n个孤立的连通分支,并将所有的边按权从大到小排 边权递减的顺序,如果加入边的两个端点不在同一个根节点的话加入,并且要将其连通,否 ...
随机推荐
- dubbo序列化
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化. dubbo 支持多种序列化方式并且序列化是和协议相对应的.比如:dubbo协议的 dub ...
- mips体系堆栈回溯分析与实现
转载:http://www.sohu.com/a/118385096_468740 mips栈帧原理 Call stack 是指存放某个程序的正在运行的函数的信息的栈.Call stack 由 sta ...
- Postfix 邮件服务 - 基础服务
环境 centos 6.5 x64 测试 IP:172.16.2.18 1.关闭selinux # cat /etc/selinux/config SELINUX=disabled 2.配置 ipta ...
- apache - http
apahce 添加模块编译 httpd # so模块用来提供DSO支持的apache核心模块 # 如果编译中包含任何DSO模块,则mod_so会被自动包含进核心. # 如果希望核心能够装载DSO, ...
- adb的使用
前面配置了环境变量,可以在计算机任何位置打开cmd窗口使用adb. 连接android应用 使用connect命令连接盒子的ip(要确保电脑所连接的网络和盒子是一个网络) 抓日志 抓取某一个操作过程的 ...
- Android Studio导入系统 jar包,编译时优先于查找系统SDK
https://www.cnblogs.com/bluestorm/p/6744140.html
- js数组的操作push,pop,shift,unshift
push(args)可以每次压入多个元素,并返回更新后的数组长度. var oldArr=[1,2,3]; alert(oldArr.push(4,[5,6]))–>5(这里只会将[5,6]当做 ...
- 【转】Python数据类型之“数字(numerics)”
[转]Python数据类型之“数字(numerics)” 上一节内容说的是“Python基本语法”,本节主要讲下Python中的数据类型. 存储在内存中的数据通常有两个属性: 在内存中的存放位置:这个 ...
- T_RegionNDS表创建及值
-- Table structure for t_regionnds -- ---------------------------- DROP TABLE IF EXISTS t_regionnds; ...
- 深入理解linux内核v4l2框架之videobuf2【转】
转自:https://blog.csdn.net/ramon1892/article/details/8444193 Videobuf2框架 1. 什么是videobuf2框架? 它是一个针对多媒体设 ...