poj 3723
来源:互联网 发布:薄洗来 知乎 编辑:程序博客网 时间:2024/05/14 04:55
题意:
想要征m个女兵,n个男兵。每个兵需要10000。但是若兵是恋情关系,且他们的恋情是有价格的,那么征另一名与他(她)有关系的兵,只需10000 - x(他们的价值)。
这个题明显最大生成树,我们把恋人的关系看成有权值的边。那么我们要找的是由这些边组成的最大的生成森林。需要注意的是不能有环,也就是,不能重复边(每个人可能有多种关系,且可能关系重复)。
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;const int maxn = 5 * 10000 + 10;const int N = 10000;struct node{ int x, y, cost; node(int a, int b, int c): x(a), y(b), cost(c){} node(){} bool operator < (const node &a) const { return cost > a.cost; }}e[maxn];int par[2 * 10000 + 5];int root(int x){ if(par[x] != x) { par[x] = root(par[x]); } return par[x];}void unite(int a, int b){ par[root(a)] = root(b);}bool same(int a, int b){ if(root(a) == root(b)) return true; return false;}int Kruskal(int n){ int ret = 0; sort(e, e + n); for(int i = 0; i < n; i++) { //printf("yes\n"); if(!same(e[i].x, e[i].y + N)) { unite(e[i].x, e[i].y + N); ret += e[i].cost; } } return ret;}int main(){ int T; scanf("%d", &T); for(int kase = 1; kase <= T; kase++) { int m, n, l; scanf("%d%d%d", &m, &n, &l); //printf("%d %d %d\n", m, n, l); for(int i = 0; i <= m; i++) par[i] = i; for(int i = N; i <= N + n; i++) par[i] = i; //printf("yes\n"); for(int i = 0; i < l; i++) { //printf("%d\n", i); int u, v, c; scanf("%d%d%d", &u, &v, &c); e[i] = node(u, v, c); } printf("%d\n", (m + n) * 10000 - Kruskal(l)); } return 0;}
阅读全文
0 0
- POJ 3723
- poj 3723
- poj 3723
- POJ 3723
- POJ 3723
- Poj 3723
- POJ 3723
- poj 3723
- poj 3723 Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription
- POJ-3723-Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription MST
- poj 3723 Conscription
- poj 3723 Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription
- c#子窗体与父窗体相互控制
- 2017年第23届全国青少年信息学奥林匹克竞赛分区联赛提高组初赛题解报告
- 有n个软件未被升级
- WSGI / uwsgi / uWSGI
- 欢迎使用CSDN-markdown编辑器
- poj 3723
- luogu P1294 高手去散步
- 专访智齿科技徐懿丨企服公司四要素:智能、融合、产品复杂度、客单价
- MarkDown Cheatsheet
- 现代IM系统中消息推送和存储架构的实现
- Linux运维学习笔记之八:用户管理
- MacOS C++编译初体验--Xcode/Vim教程
- POJ 1269 Intresting Lines(直线之间的关系及求交点)
- 缺少sasl.h