POJ 3723 Conscription
来源:互联网 发布:eclipse js禁用ctrl 编辑:程序博客网 时间:2024/05/16 11:40
题目链接:http://poj.org/problem?id=3723
解析:男女之间有了男女关系之后就会产生羁绊,然后花费就会降低(为什么不是男男,女女之间呢,╮(╯▽╰)╭不纯洁的异性关系)。
这题的话仔细想一想,因为m和n个点都必须选,所以最大为(m+n)*10000的花费,所以现在只需要选出权值最大的路径就行了。而且同一个连通图内不能有环,因为每个男女只能用一个关系。那么就变成了最大生成树。
注意:男女都是从0开始的,所以一定一定要注意要把男或的节点给+n
//最大生成树,仍然用kruskal#include<stdio.h>#include<string.h>#include<algorithm>#define INF 51000using namespace std;struct edge{int x,y,len;}e[INF];int n,m,r,T,parent[21000];//因为男女各10000,所以要开到20000int cmp(edge a,edge b){return a.len > b.len;}int root(int x){if(parent[x] == -1)return x;else return parent[x] = root(parent[x]);}void merge(int x,int y){x = root(x);y = root(y);parent[x] = y;}int main(){scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&r);memset(e,0,sizeof(e));memset(parent,-1,sizeof(parent));int a,b,c;for(int i = 0; i < r; i ++){scanf("%d%d%d",&a ,&b ,&c);e[i].x = a;e[i].y = b+n;//很重要,此处男女的节点要不同,否则会出错,也就是女1变成女1+ne[i].len = c;}sort(e,e+r,cmp);int ans = 0;for(int i = 0 ; i < r; i ++){if(root(e[i].x) != root(e[i].y)){merge(e[i].x ,e[i].y );ans += e[i].len;}}printf("%d\n",(m+n)*10000-ans);}return 0;}
- 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
- poj 3723 Conscription
- POJ--3723 Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription
- POJ 3723Conscription
- POJ 3723 Conscription
- Conscription POJ - 3723
- POJ 3723 Conscription
- stm32f407之GPIO(操作寄存器)
- android之Fragment(官网资料翻译)
- Windows7下命令行使用MySQL
- mac系统及xcode使用的SVN客户端升级
- Web服务器排行:Nginx超越Apache 成为全球TOP 1000网站最常用的Web Server
- POJ 3723 Conscription
- AsyncTask源码解析
- oracle复习
- 小程序(八) 字符串替换
- jsp页面之间简单明了的值传递
- 感知机章节课后习题 + 证明感知机算法收敛性
- 我希望这次我选对了,sl,应该给点成功果实给自己了
- 关于虚拟主机的小问题
- Fedora 18 下man文档不全的解决办法