刷题——Conscription POJ
来源:互联网 发布:thinkphp5 写的 cms 编辑:程序博客网 时间:2024/06/08 05:54
/*
招募n个女兵m个男兵,每个士兵的基础招募价格为10000
某个女兵和男兵有关系,招募了某个男兵那么招募对应的女兵根据这个关系可以减免一定的招募费,反之一样
每对关系只能用一次,那么可以把关系看成边,
类似最小生成树,改成最大生成树,基础招募费-最大生成树权重即是最小招募费用
*/
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node{int u;int v;int w;};
int n,m,r;
int rt[50100];
node G[50100];
bool cmp(node a,node b){
return a.w>b.w;
}
int find(int x){
if(x!=rt[x])rt[x]=find(rt[x]);
return rt[x];
}
int kr(){
int res=0,num=0;
for(int i=0;i<r;i++){
int x=find(G[i].u);
int y=find(G[i].v);
if(x!=y){
res+=G[i].w;
rt[x]=y;
if(++num==n+m-1)break;
}
}
return res;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&n,&m,&r);
for(int i=0;i<n+m;i++){
rt[i]=i;
}
for(int i=0;i<r;i++){
scanf("%d %d %d",&G[i].u,&G[i].v,&G[i].w);
G[i].v+=n;
}
sort(G,G+r,cmp);
printf("%d\n",(n+m)*10000-kr());
}
return 0;
}
招募n个女兵m个男兵,每个士兵的基础招募价格为10000
某个女兵和男兵有关系,招募了某个男兵那么招募对应的女兵根据这个关系可以减免一定的招募费,反之一样
每对关系只能用一次,那么可以把关系看成边,
类似最小生成树,改成最大生成树,基础招募费-最大生成树权重即是最小招募费用
*/
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node{int u;int v;int w;};
int n,m,r;
int rt[50100];
node G[50100];
bool cmp(node a,node b){
return a.w>b.w;
}
int find(int x){
if(x!=rt[x])rt[x]=find(rt[x]);
return rt[x];
}
int kr(){
int res=0,num=0;
for(int i=0;i<r;i++){
int x=find(G[i].u);
int y=find(G[i].v);
if(x!=y){
res+=G[i].w;
rt[x]=y;
if(++num==n+m-1)break;
}
}
return res;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&n,&m,&r);
for(int i=0;i<n+m;i++){
rt[i]=i;
}
for(int i=0;i<r;i++){
scanf("%d %d %d",&G[i].u,&G[i].v,&G[i].w);
G[i].v+=n;
}
sort(G,G+r,cmp);
printf("%d\n",(n+m)*10000-kr());
}
return 0;
}
阅读全文
0 1
- 刷题——Conscription POJ
- Conscription POJ
- Conscription POJ
- 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
- 8.11实战感悟
- Linux16.04配置
- Servlet/Jsp学习归纳思维导图
- 关于Spring-boot的debug调试
- 我问你答(1)
- 刷题——Conscription POJ
- mariadb
- 2017 GDUFS ACM暑假集训题集
- 数据结构之链表(三)
- CentOS7 搭建LAMP环境
- c/c++易错知识点整理3(引用和指针)(一)
- 阶乘之和
- I/O多路复用技术
- ios访问相机闪退解决办法