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按边从大到小排序 ...
随机推荐
- css3某些特性
在下列情况下,建议使用opacity属性而不是rgba()函数 1.实现多种颜色(元素)的半透明效果.使用opacity属性,不仅背景颜色,就连文本颜色.边框颜色都会变透明. 2.在不知道颜色的情况下 ...
- react native 之页面跳转
第一章 跳转的实现 1.component 中添加这行代码 <View style={styles.loginmain}> <Text style={styles.loginte ...
- 哈希表---线性探测再散列(hash)
//哈希表---线性探测再散列 #include <iostream> #include <string> #include <stdio.h> #include ...
- dba操作之archivelog清理
下面的命令用于校验归档日志的有效性,列出无效的归档日志,以及以何种方式清除归档日志,列出几种常用的: crosscheck archivelog all; ...
- 【JSOI 2007】建筑抢修
[题目链接] 点击打开链接 [算法] 将T2从小到大排序,当决策当前建筑修或不修时,若当前花费时间 + T1 <= T2,则修,否则判断T1是否小于之前修的 T1最大的建筑,若小于,则修,我们可 ...
- bzoj 2836 魔法树 —— 树链剖分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2836 树链剖分裸题: 写码五分钟,调码两小时,RE不断,狂交二十五遍,终于找到一处小细节—— ...
- JavaScript-Tool:jqgrid
ylbtech-JavaScript-Tool:jqgrid jqGrid 是一个用来显示网格数据的jQuery插件,文档比较全面,附带中文版本. 1.返回顶部 2.返回顶部 3.返回顶部 ...
- vue 使用font-awesome 只需两步
npm 安装font-awesome 以及需要的所有依赖 cnpm install less less-loader css-loader style-loader file-loader font- ...
- VS2008 MFC截取整个屏幕并保存为jpg格式
void CMainFrame::OnSavejpg() { // TODO: 在此添加命令处理程序代码 HWND hwnd = this->GetSafeHwnd(); //得到窗口句柄 HD ...
- Start Developing Mac Apps -- Human Interface Design 用户界面设计
Human Interface Design It’s not enough to create an app that works. Users expect Mac apps to be powe ...