poj2377 Bad Cowtractors
思路:
最大生成树。
实现:
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std; struct edge
{
int a, b, cost;
};
edge es[];
int ran[];
int par[];
int n, m, x, y, c; void init(int n)
{
for (int i = ; i < n; i++)
{
par[i] = i;
ran[i] = ;
}
} int find(int x)
{
if (par[x] == x)
return x;
return par[x] = find(par[x]);
} void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
return;
if (ran[x] < ran[y])
{
par[x] = y;
}
else
{
par[y] = x;
if (ran[x] == ran[y])
{
ran[x] ++;
}
}
} bool same(int x, int y)
{
return find(x) == find(y);
} bool cmp(const edge & a, const edge & b)
{
return a.cost > b.cost;
} int kru()
{
init(n);
sort(es, es + m, cmp);
int res = , cnt = ;
for (int i = ; i < m; i++)
{
if (!same(es[i].a, es[i].b))
{
unite(es[i].a, es[i].b);
cnt += ;
res += es[i].cost;
}
}
return cnt < n - ? - : res;
} int main()
{
scanf("%d %d", &n, &m);
for (int i = ; i < m; i++)
{
scanf("%d %d %d", &x, &y, &c);
es[i].a = x;
es[i].b = y;
es[i].cost = c;
}
int tmp = kru();
printf("%d\n", tmp);
return ;
}
poj2377 Bad Cowtractors的更多相关文章
- [POJ2377]Bad Cowtractors(最大生成树,Kruskal)
题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリ ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 43 Solved: ...
- BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 53 Solve ...
- 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 69 Solved: ...
- poj 2377 Bad Cowtractors (最大生成树prim)
Bad Cowtractors Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 -- 最大生成树
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MB Description 奶牛贝 ...
- POJ-2377 Bad Cowtractors---最大生成树
题目链接: https://vjudge.net/problem/POJ-2377 题目大意: 给一个图,求最大生成树权值,如果不连通输出-1 思路: kruskal算法变形,sort按边从大到小排序 ...
随机推荐
- File.Copy的时候Could not find a part of the path
https://developercommunity.visualstudio.com/content/problem/378265/filecopy-did-not-throw-the-correc ...
- Android Studio运行出现项目 错误:非法字符:“\ufeff”
导入项目时,遇到编码问题. 主要原因是: eclipse可以自动把UTF-8+BOM文件转为普通的UTF-8文件,但Android Studio需要重新转换一下编码 解决方法: 将编码格式UTF-8+ ...
- 命令行 sql 将结果导出到文件
1. into outfile filename; Mysql查询结果导出为Excel的几种方法 mysql> use world; # 使用 mysql 自带的 world 数据库 mysql ...
- CodeForces-380C:Sereja and Brackets(线段树与括号序列)
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...
- AtCoder Beginner Contest 057
A题 分析:24进制转换 #include<iostream> using namespace std; int main() { int a,b; while(cin>>a& ...
- C++实现利用(前序和中序生成二叉树)以及(二叉树的镜像)
#include<iostream> #include<string.h> #include<stack> using namespace std; typedef ...
- hadoop2 Ubuntu 下安装部署
搭建Hadoop环境( 我以hadoop 2.7.3 为例, 系统为 64bit Ubuntu14.04 ) hadoop 2.7.3 官网下载 , 选择自己要安装的版本.注意每个版本对应两个下载选项 ...
- File System Programming---(三)
Accessing Files and Directories Before you can open a file, you first have to locate it in the file ...
- python: 使用matplotlib的pyplot绘制图表
工作中需要观察数据的变化趋势,用python写了一段小程序来用显示简单图表,分享出来方便有同样需求的人,matplotlib是个很不错的库. #!encode=utf8 from matplotlib ...
- js遍历ajax回调函数返回值中的object对象
function printObject(obj) { //obj = {"cid":"C0","ctext":"区县& ...