Conscription-最小生成树-Kruskal
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
2 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 5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133
Sample Output
71071
54223
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int n,m,r;
int par[]; struct edge
{
int u;
int v;
int cost;
};
edge es[]; void init(int n)
{
memset(par,,sizeof(par));
for(int i=;i<n;i++)
par[i]=i;
} int seek(int x)
{
if(x==par[x])
return x;
else
return par[x]=seek(par[x]);
} void unite(int x,int y)
{
int xx=seek(x);
int yy=seek(y);
if(xx!=yy)
par[xx]=yy;
} bool same(int x,int y)
{
int xx=seek(x);
int yy=seek(y);
return (xx==yy);
} int boss(int n)//找有多少个集合,即有多少连通图
{
int res=;
for(int i=;i<n;i++)
{
if( seek(i)==i )
res++;
}
return res;
} bool cmp(edge e1,edge e2)
{
return e1.cost<e2.cost;
} int Kruskal()
{
sort(es,es+r,cmp);
int res=;
for(int i=;i<r;i++)
{
edge e=es[i];
if(!same(e.u,e.v))
{
unite(e.u,e.v);
res+=e.cost;
}
}
return res;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&r);//女,男,关系数,人当做点,从0开始
int x,y,d;
init(n+m);
for(int i=;i<r;i++)
{
scanf("%d%d%d",&x,&y,&d);
es[i].u=x;
es[i].v=(n+y);//把男女都当做一类点
es[i].cost=-d;
}
int ans=Kruskal();
int num=boss(n+m);
ans+=num*;
printf("%d\n",ans);
}
return ;
}
Conscription-最小生成树-Kruskal的更多相关文章
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- 最小生成树 kruskal算法 codevs 1638 修复公路
1638 修复公路 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description A地区在地震过后,连接所有村庄的公 ...
- 最小生成树——kruskal算法
kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...
- 贪心算法-最小生成树Kruskal算法和Prim算法
Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...
- 最小生成树---Kruskal/Prime算法
1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图. 方法:将图中边按其权值由小到大的次序顺序选取,若选边后不 ...
- 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组
2177: 曼哈顿最小生成树 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 190 Solved: 77[Submit][Status][Discu ...
- 最小生成树Kruskal算法(邻接矩阵和邻接表)
最小生成树,克鲁斯卡尔算法. 算法简述: 将每个顶点看成一个图. 在所有图中找权值最小的边.将这条边的两个图连成一个图, 重复上一步.直到只剩一个图. 注:将abcdef每个顶点看成一个图.将最小权值 ...
随机推荐
- 你创建的OpenStack高性能虚拟机能实现“零损耗”么?
使用默认参数创建的虚拟机,虚拟机的VCPU在物理CPU不同核心之间动态调度,另外,由于Linux还可能会将软中断,内存交换等进程调度到虚拟机正在使用的物理核心上,这些因素导致这些虚拟机相对于物理机的计 ...
- Solr游标查询提高翻页效率
长期以来,我们一直有一个深分页问题.如果直接跳到很靠后的页数,查询速度会比较慢.这是因为Solr的需要为查询从开始遍历所有数据.直到Solr的4.7这个问题一直没有一个很好的解决方案.与最近发布的So ...
- es6基础(7)--函数扩展
{ //有默认值的后面如果有参数必须要有默认值 function test(x,y="world"){ console.log(x,y) } test('hello');//hel ...
- es6基础(3)-正则扩展
//正则扩展 { let regex=new RegExp('xyz','i'); let regex2=new RegExp(/xyz/i); console.log(regex.test('xyz ...
- 【Fiddler学习】Fiddler简介和Web抓包应用(转)
一.Fiddler是什么? Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的进出Fiddler的数据. Fiddler 要比其 ...
- C#取整函数Math.Round、Math.Ceiling和Math.Floor
1.Math.Round:四舍六入五取偶 引用内容 Math.Round(0.0) //0Math.Round(0.1) //0Math.Round(0.2) //0Math.Round(0.3) / ...
- Heap Allocation Profiles (heap=sites)
基于JDK-6,使用的类不同,展示的信息可能不同,内容一样的javac -J-agentlib:hprof=heap=sites Hello.java percent live alloc’ed st ...
- 432 4.3.2 STOREDRV.Deliver; recipient thread limit exceeded
最近几天Hub-Mailbox服务器时不时就CPU超过90%.在任务管理器里面看到edgetransport占用大量CPU.进入EMC的队列查看器,看到邮箱数据库堵塞,队列上万. 堵塞的邮件大多是收件 ...
- 1. ibatis 查询的sql列存在相同的列名
如果SQL语句存在两个相同的查询列名,则映射时,取第一个列名的值进行映射 <?xml version="1.0" encoding="UTF-8" ?&g ...
- PHP微信关注自动回复文本消息。
服务器配置URL默认接受 $_GET["echostr"] 配置成功. public function GetShow(){ $token = $this->token; / ...