题意:给出一个图,求出其中的最大生成树= =如果无法产生树,输出-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)的更多相关文章

  1. poj 2377 Bad Cowtractors

    题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...

  2. poj - 2377 Bad Cowtractors&&poj 2395 Out of Hay(最大生成树)

    http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她 ...

  3. poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  4. POJ - 2377 Bad Cowtractors Kru最大生成树

    Bad Cowtractors Bessie has been hired to build a cheap internet network among Farmer John's N (2 < ...

  5. POJ 2377 Bad Cowtractors( 最小生成树转化 )

    链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...

  6. poj 2377 Bad Cowtractors(最大生成树!)

    Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...

  7. POJ:2377-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  8. MST:Bad Cowtractors(POJ 2377)

    坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...

  9. poj 2377 拉最长的线问题 kruskal算法

    题意:建光纤的时候,拉一条最长的线 思路:最大生成树 将图的n个顶点看成n个孤立的连通分支,并将所有的边按权从大到小排 边权递减的顺序,如果加入边的两个端点不在同一个根节点的话加入,并且要将其连通,否 ...

随机推荐

  1. golang锁

    golang锁包:https://studygolang.com/pkgdoc sync.Mutex是一个互斥锁 var lock sync.Mutex 加锁段在中 lock.lock() lock. ...

  2. java中import机制(指定import和import *的区别)

    转自:https://www.cnblogs.com/dtts/p/4692480.html java中有两种包的导入机制,总结如下: 单类型导入(single-type-import),       ...

  3. webx roadmap

    SpringExt 自定义Spring Schema的例子 基于Spring可扩展Schema提供自定义配置支持 使用SpringExt扩展Webx的示例 扩展点和捐献 一个namespace下可以声 ...

  4. VUE2.0 饿了吗视频学习笔记(四):颜色、跳转、设置、vue-resource

    https://gitee.com/1981633/vue_study.git 源码下载地址,随笔记动态更新中 1.设置选中项颜色 <template> <div id=" ...

  5. Linux - 系统资源

    查看剩余内存 free -m #-/+ buffers/cache: #6458M为真实使用内存 1649M为真实剩余内存(剩余内存+缓存+缓冲器) #linux会利用所有的剩余内存作为缓存,所以要保 ...

  6. Android 常用 adb 命令总结【转】

    原文链接 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Android 测试带来很大的方便,其中很多命令将会用于自动化测试的脚本当中 ...

  7. css scrollbar样式设置

    参考链接:https://segmentfault.com/a/1190000012800450

  8. 爬虫之验证码IP攻防心得——小总结

    小前言: 一般来说,现在很多平台注册.登录的时候会涉及到验证码,这样做的目的是为了防止恶意程序恶意访问,从而给服务器造成一定的压力,会浪费一定的资源,大家也都知道,现在这种短信平台,邮箱平台等都是收费 ...

  9. 理解 Linux 配置文件【转】

    转自:http://www.ibm.com/developerworks/cn/linux/management/configuration/ 分类和使用 本文说明了 Linux 系统的配置文件,在多 ...

  10. unicode 和 utf-8字符编码的区别

    作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有,转载请联系作者获得授权.   原文:unico ...