POJ-3723-Conscription
来源:互联网 发布:网络信息采集技术 编辑:程序博客网 时间:2024/05/16 11:46
这个题大意是说需要征募女兵N人,男兵M人。每征募一个人需要花费10000美元。但是如果已经征募的人中有一些关系亲密的人,那么可以少花一点钱。题目中给你R个男女之间的亲密度关系,如果a号男和m号女有亲密度关系,那么只要现在招募到他们中的一个人,那么招募另外一个人的花费将变为10000-他们之间的亲密度。然后要求你求出招募到所有人的最小花费。
思路:其实应该不太难想到要用最小生成树解决这个题目,由于题目男女都是0开始编号,所以我们首先应该对男女进行重新编号,我处理的方法是将女的编号在男的后。然后对于每个关系,我们建立对应一条边,然后求出最小生成树,最后再算出多少人没有被招到即。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=50000;struct Node{ int from; int to; int c; bool operator <(const Node& a)const {return c<a.c; }}a[maxn];int n,m,k,p[maxn];void Init(){ for(int i=0;i<=n+m;i++)p[i]=-1;}int find(int x){ if(p[x]==-1)return x; return p[x]=find(p[x]);}int main(){ int T; scanf("%d",&T); while(T--) {scanf("%d%d%d",&n,&m,&k);Init();for(int i=0;i<k;i++){ int u,v,c; scanf("%d%d%d",&u,&v,&c); a[i].from=u; a[i].to=v+n; a[i].c=10000-c;}sort(a,a+k);int ans=0,cnt=0;for(int i=0;i<k;i++){ int x=find(a[i].from); int y=find(a[i].to); if(x==y)continue; p[x]=y; ans+=a[i].c; cnt++;}ans+=(n+m-cnt)*10000;printf("%d\n",ans); } return 0;}
0 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
- 第一章 概述 1.2分层
- Day3报告
- Javascrip中innerHTML使用方法
- 敏捷测试(4)--基于story的敏捷基础知识
- Eclipse Launcher (Run/Debug As 菜单扩展)实现
- POJ-3723-Conscription
- 数组指针 指针数组 函数指针 指针函数
- 阅读Qt demo笔记——关于图像视图框架和状态机框架
- 修改MySQL最大连接数
- 黑马程序员-学习日志-文件的合并
- linux网络协议栈(五)网络层 (4)路由表
- Linux tar exclude参数的用法
- java编程--心有灵犀一点通
- Javascrip阻止表单提交刷新页面