poj 3723 Conscription
来源:互联网 发布:企业经济数据 编辑:程序博客网 时间:2024/05/10 02:36
好忧伤,根本就是同一种算法,为毛我的代码不能过啊!!!
就是kruskal的最大生成树变形。
Description
Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.
Input
The first line of input is the number of test case.
The first line of each test case contains three integers, N, M and R.
Then R lines followed, each contains three integers xi, yi and di.
There is a blank line before each test case.
1 ≤ N, M ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000
Output
Sample Input
25 5 84 3 68311 3 45830 0 65920 1 30633 3 49751 3 20494 2 21042 2 7815 5 102 4 98203 2 62363 1 88642 4 83262 0 51562 0 14634 1 24390 4 43733 4 88892 4 3133
Sample Output
7107154223
Source
我的WA了:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn=20000;int father[maxn+10],rank[maxn+10];void init(){ for(int i=0;i<maxn+10;i++) { father[i]=i; rank[i]=0; }}int find(int x){ if(father[x]==x) return x; else return father[x]=find(father[x]);}void unite(int x,int y){ x=find(x); y=find(y); if(x==y) return; if(rank[x]<rank[y]) { father[x]=y; } else { father[y]=x; if(rank[x]==rank[y]) rank[x]++; }}struct edge{ int from; int to; int cost;};edge G[50000+10];bool cmp(const edge & a,const edge & b){ return a.cost>b.cost;}int t;int main(){ //freopen("x.in","r",stdin); cin>>t; while(t--) { init(); int n,m,r; scanf("%d%d",&n,&m); scanf("%d",&r); for(int i=0;i<r;i++) { scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost); G[i].to+=m; } sort(G,G+r,cmp); int sum=0; for(int i=0;i<r;i++) { if(find(G[i].from)!=find(G[i].to)) { unite(G[i].from,G[i].to); sum+=G[i].cost; } } printf("%d\n",10000*(n+m)-sum); } return 0;}
别人的。。。AC了:
#include <cstdio>#include <algorithm>#define N 20005#define E 50005using namespace std;struct edge{ int u,v,w;}e[E];int n,sum;int father[N],rank[N];bool cmp(edge a,edge b){ return a.w>b.w;}void makeset(void){ register int i=0; for(i=0;i<n;i++) { father[i]=i; rank[i]=0; }}int find(int x){ if(x!=father[x]) father[x]=find(father[x]); return father[x];}void Union(int x,int y,int w){ x=find(x),y=find(y); if(x==y) return; if(rank[x]>rank[y]) father[y]=x; else { if(rank[x]==rank[y]) rank[y]++; father[x]=y; } sum+=w;}int main(void){ int m,r,t,a,b,c; register int i; scanf("%d",&t); while(t--) { scanf("%d%d%d",&m,&n,&r); n+=m; makeset(); for(i=0;i<r;i++) { scanf("%d%d%d",&a,&b,&c); b+=m; e[i].u=a,e[i].v=b,e[i].w=c; } sort(e,e+r,cmp); for(sum=i=0;i<r;i++) Union(e[i].u,e[i].v,e[i].w); printf("%d\n",10000*n-sum); } 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
- 给程序员简历的一些建议 .
- Oracle 中的Userenv()
- 黑马程序员 自己动手写类的索引指示器
- 编程算法 - 圆圈中最后剩下的数字(循环链表) 代码(C++)
- 定义一个数组,编程打印它的全排列。
- poj 3723 Conscription
- 机器学习资源(网址)
- 简单实现FTP下载
- Java中的==和equals区别
- Color the ball
- C++ 重载运算符
- 学习笔记_毕向东 Java_继承_抽象类_接口_多态 2014.7.13
- ubuntu设置声音大小
- 高精度小数相加