poj 3723 kruscal,反边取最大生成树。
来源:互联网 发布:爱知工科大学怎么样 编辑:程序博客网 时间:2024/06/08 15:08
题意:
需要征募女兵N人,男兵M人。
每征募一个人需要花费10000美元,但是如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱。
给出若干的男女之间的1~9999之间的亲密关系度,征募某个人的费用是10000 - (已经征募的人中和自己的亲密度的最大值)。
要求通过适当的招募顺序使得征募所有人的费用最小。
解析:
先设想无向图,在征募某个人a时,如果使用了a和b之间的关系,那么就连一条a到b的边。
假设这个图中存在圈,那么无论以什么顺序征募这个圈上的所有人,都会产生矛盾。
因此可以知道这个图是一片森林。
反之,如果给了一片森林,那么就可以使用对应的关系确定征募的顺序。
因此,把人看做顶点,关系看做边,这个问题就可以转化为求解无向图中的最大权森林问题。
最大权森林问题可以通过把所有边权取反(-号)之后用最小生成树去求解。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 20000 + 10;int fa[maxn];int n, m, r;struct edge{ int fr, to; int w;} e[maxn << 2];bool cmp(edge a, edge b){ return a.w < b.w;}int find(int x){ if(x != fa[x]) fa[x] = find(fa[x]); return fa[x];}int kruscal(){ int res = 0; sort(e, e + r, cmp); for(int i = 0; i <= n + m; i++) fa[i] = i; for(int i = 0; i < r; i++) { int t1 = find(e[i].fr); int t2 = find(e[i].to); if(t1 != t2) { fa[t2] = t1; res += e[i].w; } } return res;}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL int ncase; scanf("%d", &ncase); while (ncase--) { scanf("%d%d%d", &n, &m, &r); for (int i = 0; i < r; i++) { scanf("%d%d%d", &e[i].fr, &e[i].to, &e[i].w); e[i].to = e[i].to + n; e[i].w = -e[i].w; } printf("%d\n", 10000 * (n + m) + kruscal()); } return 0;}
0 0
- poj 3723 kruscal,反边取最大生成树。
- poj 3723 Conscription(最大生成树 kruscal)
- poj 1797 (最大生成树Kruscal)
- (kruscal12.3.5)POJ 2485 Highways(使用kruscal来计算最小生成树的最大边)
- 最小生成树 Kruscal算法--poj 2377
- 最小生成树 Kruscal
- 最小生成树~kruscal
- 最小生成树-kruscal
- poj Out of hay 最小生成树 kruscal
- POJ 1287 Networking(kruscal 最小生成树)
- POJ 1789最小生成树(kruscal算法)
- poj 1287 Networking(prim or kruscal最小生成树)
- poj 2485 Highways(kruscal or prim最小生成树)
- POJ 1861 && ZOJ 1542--Network 【最小生成树 && kruscal && 水题】
- POJ 2349 Arctic Network 最小生成树 prim && kruscal
- 最小生成树-Kruscal-POJ 1258 Agri-Net
- POJ 2395 Out of Hay (Kruscal最小生成树)
- POJ 2075 Tangled in Cables(最小生成树 kruscal)
- addmrpt.sql
- 利用正则表达式除去html得到纯文本
- ashrpt.sql
- 【树链剥分】专题小结
- [php] 静态方法 接口 链式操作
- poj 3723 kruscal,反边取最大生成树。
- 安装keepalived 报错`install' is up to date
- 设计模式(十八)之 Strategy(策略)
- ThinkPHP3.2使用PHPMailer发送邮件
- HDU ACM 4515 小Q系列故事——世界上最遥远的距离
- 英语学习电影资源
- 面试题:Java中对象序列化接口(Serializable)的意义
- 【重构】dll文件是什么
- 无需光驱恢复或重置系统(正常会提示插入介质)