COJ 0580 4021征兵方案
2015-07-24 19:08
381 查看
4021征兵方案 |
难度级别: C; 编程语言:不限;运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B |
试题描述 |
现在需要征募女兵N人,男兵M人,每征募一个人需要花费10000美元,但如果已经征募的人中有一些关系亲密的人,就可以少花些钱。给出若干男女之间的亲密关系值(用1到9999表示),则征募某个人的费用为10000减去已征募人中与该人亲密值的最大值。要求通过适当的征募顺序安排使得征募所有人所需的费用最小。 |
输入 |
第一行包括三个数N,M和R,接下来的R行,每行包括三个数x,y和d,表示第x号男兵和第y号女兵之间的亲密度为d,各行的数两两之间用一个空额分隔。 |
输出 |
一个数,表示征兵的费用。 |
输入示例 |
5 5 8 4 3 6831 1 3 4583 0 0 6592 0 1 3063 3 3 4975 1 3 2049 4 2 2104 2 2 781 |
输出示例 |
71071 |
其他说明 |
数据范围:1<=N,M<=10000,0<=R<=50000,0<d<10000,0<=x<M,0<=y<N. |
最后认真画了一下图,妈妈呀,这不就是个裸生成树嘛。。。。。。。。。。。。。。。。。。。
哭瞎了。。。
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<cstring> #define PAU putchar(' ') #define ENT putchar('\n') using namespace std; const int maxn=50000+10; struct node{ int from,to,c; bool operator <(const node&a)const{return c<a.c;} }a[maxn];int n,m,k,p[maxn]; int find(int x){return p[x]==x?x:p[x]=find(p[x]);} inline int read(){ int x=0,sig=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0; for(;isdigit(ch);ch=getchar())x=10*x+ch-'0'; return sig?x:-x; } inline void write(int x){ if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; } void init(){ n=read();m=read();k=read(); for(int i=0;i<=n+m;i++)p[i]=i; for(int i=0;i<k;i++){ int u=read(),v=read(),c=read(); a[i]=(node){u,v+n,10000-c}; } sort(a,a+k);int ans=0,cnt=0; for(int i=0;i<k;i++){ int x=find(a[i].from),y=find(a[i].to); if(x!=y)p[x]=y,ans+=a[i].c,cnt++; } ans+=(n+m-cnt)*10000; write(ans); return; } void work(){ return; } void print(){ return; } int main(){init();work();print();return 0;}
相关文章推荐
- 哈哈哈
- SQLite数据库---将复杂对象存入数据库
- A. 简单的生活(cin)
- hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语
- json
- MongoDB启动方法
- 地图定位出错Exception : 'Invalid Region
- Myeclipse如何自动创建hibernate
- Codeforces Round #313 A. Currency System in Geraldion
- 认识JAVA
- 北航11年机试--求指定区间内的孪生数
- 将博客搬至CSDN
- A - Jungle Roads - poj 1251(简单)
- 协议
- DTM中Channel的顺序与Cfg中Module顺序不一致会不会影响V0通信?
- 类的扩展
- Hdu 1856(离散化+并查集)More is better
- hdu 2898 旋转 三维空间一点绕一轴旋转后坐标 几何
- 下来框取的被选中的项的值
- Fragment间的通信