http://poj.org/problem?id=3723

这题虽然简单,但是还是错了很多次。

因为这题构建的图可能是不连通的。也就是说可能有很多棵树。

所以我以前写的并查集用在这上面会出问题的。

while(x != f[x])

x = f[x];

return f[x];

//我这样子每次用完之后并没有更新f[x]的值。

//虽然在连通图中没问题,但是在不连通的图里用就会有问题啦。 血的教训。。。。

改正:

if(x !=f[x])

f[x] = find(f[x]);

return f[x];

//============================================

code:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 20050;
const int maxm = 50050;
struct edge {
int x, y, w;
bool operator < (const edge& rhs) const {
return w > rhs.w;
}
}; edge es[maxm];
int f[maxn];
int n, m; int Find(int x)
{
if(x !=f[x])
f[x] = Find(f[x]);
return f[x];
} int kruskal() {
int i, a, b;
int ans = 10000*n;
for(i=0; i<=n; ++i) f[i] = i;
sort(es, es+m);
for(i=0; i<m; ++i) {
a = Find(es[i].x);
b = Find(es[i].y);
if(a!=b) {
f[a] = b;
ans -= es[i].w;
}
}
return ans;
}
int main() {
int N, M, R, i, t;
scanf("%d",&t);
while(t--) {
scanf("%d%d%d",&N,&M,&R);
n = N+M;
m = R;
for(i=0; i<m; ++i) {
scanf("%d%d%d",&es[i].x, &es[i].y, &es[i].w);
es[i].y += N;
}
printf("%d\n", kruskal());
}
return 0;
}

POJ3723 Conscription的更多相关文章

  1. POJ3723 Conscription 【并检查集合】

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8071   Accepted: 2810 Desc ...

  2. Conscription poj3723(最大生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6870   Accepted: 2361 Desc ...

  3. Conscription [POJ3723] [最小生成树]

    Description: Windy有一个国家,他想建立一个军队来保护他的国家. 他召集了N个女孩和M男孩,想把他们雇佣成为他的士兵. 要无偿雇佣士兵,必须支付10000元. 女孩和男孩之间有一些关系 ...

  4. 【POJ - 3723 】Conscription(最小生成树)

    Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...

  5. POJ 3723 Conscription 最小生成树

    题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...

  6. POJ 3723 Conscription

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6325   Accepted: 2184 Desc ...

  7. Conscription

    Conscription Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  8. POJ-3723 Conscription---最大权森林---最小生成树

    题目链接: https://vjudge.net/problem/POJ-3723 题目大意: 需要征募女兵N人, 男兵M人. 每征募一个人需要花费10000美元. 带式如果已经征募的人中有一些关系亲 ...

  9. 《挑战程序设计竞赛》2.5 最小生成树 POJ3723 3169 1258 2377 2395 AOJ2224(1)

    POJ3723 http://poj.org/problem?id=3723 题意 windy要组建一支军队,召集了N个女孩和M个男孩,每个人要付10000RMB,但是如果一个女孩和一个男孩有关系d的 ...

随机推荐

  1. JS操作SELECT方法

    1.判断select选项中 是否存在Value="paraValue"的Item2.向select选项中 加入一个Item3.从select选项中 删除一个Item4.修改sele ...

  2. php生成员工编号,产品编号

    由于某些原因需要获取数据库最大的id值.所以出现了这段php 获取数据库最大的id代码了.这里面的max(id) 这里面的id 就是要获取最大的id了.如果是别的字段请填写为其他字段 获取数据库中最大 ...

  3. Attributes(2): Displaying attributes for a class.(显示类属性)

    输出类属性   using System; using System.Reflection;   namespace Attribute02 { //用于Class和Struct类型 [Attribu ...

  4. ajax请求遇到服务器重启或中断

    常会有不断轮询发送ajax请求,处理一些业务的场景. 要考虑到: 1. 服务器重启,中断,恢复后仍然能恢复正常业务处理. 服务器重启过程中,再次发送请求,请求状态将变为net::ERR_CONNECT ...

  5. linux源码Makefile的详细分析

    目录 一.概述 1.本文的意义 2.Linux内核Makefile文件组成 二.Linux内核Makefile的“make解析”过程 1 顶层Makefile阶段 1.从总目标uImage说起 2.v ...

  6. c#中的整形类型

    一.整型类型 C#中定义了8中整数类型:字节型(byte).无符号字节型(ubyte).短整型(short).无符号短整型(ushort).整型(int).无 符号整型(uint).长整型(long) ...

  7. 十大响应式Web设计框架

    http://www.csdn.net/article/2014-05-13/2819739-responsive-frameworks-for-web-design 对于设计师而言,网站设计中的任意 ...

  8. In machine learning, is more data always better than better algorithms?

    In machine learning, is more data always better than better algorithms? No. There are times when mor ...

  9. ireport常见问题

    $V{PAGE_NUMBER} 表示当前是第几页 ,在text field 的 选项evaluation time选report是共几页,now表是当前页.页码可在ireport里直接设置. &quo ...

  10. [MVC] WebSecurity在VS2013中不识别

    使用VS2013创建了MVC4项目以后,在filters中添加了类InitializeSimpleMembershipAttribute ,不能识别UserContext和WebSecurity,发现 ...