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. Elasticsearch客户端大全 http://www.searchtech.pro/elasticsearch-clients

    Elasticsearch有各种语言的客户端,下面一一列出来: Perl ElasticSearch.pm: Perl客户端 Python pyes: Python客户端pyelasticsearch ...

  2. 使用Markdown在博客里插入代码

    今天尝试了一下在线使用Markdown编辑器写博客,发现想要实现下面这样的效果还真得折腾一会儿. <html> <head> <meta charset="ut ...

  3. 这篇blog只是为了发一张图链到UOJ的博客去..

    UOJ卖萌表情,萌萌哒VFK如图.

  4. GCD介绍(一): 基本概念和Dispatch Queue

    什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像NSOperationQueue,他们都允许 ...

  5. mysql grant 示例

    ' with grant option; FLUSH PRIVILEGES;

  6. VC和VS调用Lua设置以及Lua C API使用。

    通过c++调用lua 脚本,    环境VC++6.0    lua  sdk 5.1.4 在调用前先认识几个函数.1.调用lua_open()将创建一个指向Lua解释器的指针.2. luaL_ope ...

  7. buffer busy waits

    Buffer busy waits 当会话想要访问缓冲区中的数据块,而该数据块正在被其他会话使用时将产生Buffer busy waits事件. 其他会话可能正从数据文件向缓冲器读取同样的数据块,或正 ...

  8. 使用bacula实现Linux的远程备份和还原

    Bacula,被誉为开源软件中最好的备份还原软件,它提供了企业级的客户机/服务器的备份解决方案,能够通过网络来管理文件的备份,恢复和核实工作.Bacula,既有windows版本的,也有Linux,U ...

  9. PHP Sessions子系统会话固定漏洞

    漏洞名称: PHP Sessions子系统会话固定漏洞 CNNVD编号: CNNVD-201308-193 发布时间: 2013-08-22 更新时间: 2013-08-22 危害等级: 中危   漏 ...

  10. Node.js权威指南 (11) - 加密与压缩

    11.1 加密与解密处理 / 295 11.1.1 crypto模块概述 / 295 11.1.2 散列算法 / 296 11.1.3 HMAC算法 / 297 11.1.4 公钥加密 / 29811 ...