最大生成树

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn = 50000 + 131;
const int maxm = 10000 + 131;
struct Edge {
int u, v, cost;
Edge(int u_, int v_, int c_): u(u_), v(v_), cost(c_) {}
bool operator < (const Edge a) const {
return cost > a.cost;
}
};
vector<Edge> G; /// Uinon-Set
int Pre[maxm * 2], Num[maxm * 2];
void Init(int N) {
for(int i = 0; i <= N; ++i)
Pre[i] = i;
} int Find(int x) {
/*int r = x;
while(r != Pre[r]) r = Pre[r];
return Pre[x] = r;*/
if(x == Pre[x]) return x;
else return Pre[x] = Find(Pre[x]);
} bool Union(int x, int y) {
int ax = Find(x), ay = Find(y);
if(ax == ay) return false;
Pre[ax] = ay;
return true;
} /// MST;
typedef long long LL;
LL Sum = 0;
LL Kusual(int N,int R)
{
Sum = 0;
sort(G.begin(),G.end());
Init(N);
for(int i = 0; i < G.size(); ++i)
{
int u = G[i].u, v = G[i].v;
if(Union(u, v))
{
Sum +=(LL) (G[i].cost);
}
}
return Sum;
} int main()
{
int N, M, R;
int x, y, d;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d", &N, &M, &R);
G.clear();
for(int i = 0; i < R; ++i)
{
scanf("%d%d%d", &x, &y, &d);
G.push_back(Edge(x,y+N,d));
G.push_back(Edge(y+N,x,d));
}
//cout << Sum << endl;
//Sum = 0;
printf("%lld\n",(LL)(10000 * (N+M)) - Kusual(N+M,R));
}
}

POJ 3723的更多相关文章

  1. poj - 3723 Conscription(最大权森林)

    http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他 ...

  2. POJ 3723 Conscription(并查集建模)

    [题目链接] http://poj.org/problem?id=3723 [题目大意] 招募名单上有n个男生和m个女生,招募价格均为10000, 但是某些男女之间存在好感,则招募的时候, 可以降低与 ...

  3. POJ 3723 Conscription MST

    http://poj.org/problem?id=3723 题目大意: 需要征募女兵N人,男兵M人,没征募一个人需要花费10000美元,但是如果已经征募的人中有一些关系亲密的人,那么可以少花一些钱, ...

  4. POJ 3723 Tree(树链剖分)

    POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...

  5. MST:Conscription(POJ 3723)

      男女搭配,干活不累 题目大意:需要招募女兵和男兵,每一个人都的需要花费1W元的招募费用,但是如果有一些人之间有亲密的关系,那么就会减少一定的价钱,如果给出1~9999的人之间的亲密关系,现在要你求 ...

  6. POJ 3723 Conscription 最小生成树

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

  7. POJ 3723 Conscription

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

  8. POJ 3723 Conscription【最小生成树】

    题意: 征用一些男生和女生,每个应都要给10000元,但是如果某个男生和女生之间有关系,则给的钱数为10000减去相应的亲密度,征集一个士兵时一次关系只能使用一次. 分析: kruskal求最小生成树 ...

  9. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

随机推荐

  1. python 调用 java代码

    一.JPype简述 1.JPype是什么? JPype是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python 在某些领域(如服务器端编程)中的不足. 2.JPype ...

  2. CRM项目之RBAC权限组件-day26

    写在前面 上课第26天,打卡: 世间安得双全法 不负如来不负卿 s17day26 CRM项目 项目概要:XX公司CRM - 权限管理,公共组件,app ***** - 熟悉增删改查,Low *** - ...

  3. 前端基础之JavaScript - day14

    写在前面 上课第14天,打卡: 唯心不易,坚持! 参考:http://www.cnblogs.com/yuanchenqi/articles/6893904.html 前言 一个完整的 JavaScr ...

  4. Javaweb学习笔记——(十五)—————— sql复习

    sql复习 数据库管理系统(DBMS)的概述 1.什么是DBMS:数据的仓库 *方便查询 *可存储的数据量大 *保证数据的完整.一致 *安全可靠 2.DBMS的发展:今天主流数据库为关系型数据库管理系 ...

  5. Play XML Entities

    链接:https://pentesterlab.com/exercises/play_xxe/course Introduction This course details the exploitat ...

  6. 【SRM-09 B】撕书II

    Description 琉璃手头有一黑一白两本魔法书,一本是<缟玛瑙的不在证明>,另一本是<白色相簿1.5>.传说同时打开这两本书会有奇怪的事情发生.琉璃打开一看,果然非常奇怪 ...

  7. cpp 标准库

    源:http://bbs.csdn.net/topics/300040713 C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能.< ...

  8. Coursera, Deep Learning 5, Sequence Models, week1 Recurrent Neural Networks

    有哪些sequence model Notation: RNN - Recurrent Neural Network 传统NN 在解决sequence input 时有什么问题? RNN就没有上面的问 ...

  9. ubuntu下objective-c的编译和运行

    ubuntu 下编译objective-c 1.安装编译环境 sudo aptitude install build-essential gobjc gobjc++ gnustep gnustep-d ...

  10. linux scanf函数%d后加空白

    参考链接: https://bbs.csdn.net/topics/390389059 关键点: scanf()中空白字符(包括/n,space)会使scanf()函数在读操作中略去输入中的零个或者一 ...