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, NM and R.
Then R lines followed, each contains three integers xiyi and di.
There is a blank line before each test case.

1 ≤ NM ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000

Output

For each test case output the answer in a single line.

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的更多相关文章

  1. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  2. 最小生成树——Kruskal与Prim算法

    最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...

  3. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

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

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

  5. 最小生成树 kruskal算法 codevs 1638 修复公路

    1638 修复公路  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description A地区在地震过后,连接所有村庄的公 ...

  6. 最小生成树——kruskal算法

    kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...

  7. 贪心算法-最小生成树Kruskal算法和Prim算法

    Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...

  8. 最小生成树---Kruskal/Prime算法

    1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图.      方法:将图中边按其权值由小到大的次序顺序选取,若选边后不 ...

  9. 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组

    2177: 曼哈顿最小生成树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 190  Solved: 77[Submit][Status][Discu ...

  10. 最小生成树Kruskal算法(邻接矩阵和邻接表)

    最小生成树,克鲁斯卡尔算法. 算法简述: 将每个顶点看成一个图. 在所有图中找权值最小的边.将这条边的两个图连成一个图, 重复上一步.直到只剩一个图. 注:将abcdef每个顶点看成一个图.将最小权值 ...

随机推荐

  1. C#中唯一的三元运算符

    条件运算符?:接受三个操作数,是C#中唯一的三元运算符 ; ? : ; //转换成if选择结果如下 ) { j = ; } else { j = ; } 需要根据还可以嵌套三元运算符 ; ) ? : ...

  2. django 解决css,js文件304导致无法加载显示问题

    这种情况一般会在windows系统下出现 1.前台.后台如果无法加载css等样式.(建议通过此办法来解决) 这是因为你安装的某些IDE 或者其他更改了注册表导致的系统的注册表\HKEY_CLASSES ...

  3. 剑指offer(一):二维数组中的查找

    说明: 1.本系列是根据<剑指Offer>这个系列做的一个小笔记. 2.直接动力是因为师兄师姐找工作很难,而且机械出生的我面试算法更难. 3.刚开始准备刷LeetCode.LintCode ...

  4. 代码: html 页面小效果 (集合,待补充)

    标签切换(下部内容区跟着切换): 2016-6-2 <script type="text/javascript" src="http://cdn.bootcss.c ...

  5. 22.纯 CSS 创作出美丽的彩虹条纹文字

    原文地址:https://segmentfault.com/a/1190000014858628 感想: 利用四个span的::before 和 ::after创出多个WEB,给其颜色,绝对定位,再利 ...

  6. 11.纯 CSS 创作一个荧光脉冲 loader 特效

    原文地址:https://segmentfault.com/a/1190000014700727 HTML代码: <html> <head> <link rel=&quo ...

  7. 解决在V2.0中子组件使用v-model接收来自父组件的值异常

    当我们使用父组件向子组件传值,当子组件中是v-model使用该值时会报:[Vue warn]: Avoid mutating a prop directly since the value will ...

  8. Maven项目的生命周期

    Maven中存在三套生命周期,每一套生命周期相互独立,互不影响.在一套生命周期内,执行后面的命令前面的命令会自动执行. CleanLifeCycle:清理生命周期 mvn clean DefaultL ...

  9. 《锋利的JQuery》中重写超链接与图片提示效果

    代码部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...

  10. leetcode435

    使用贪心思想,先按照end排序,然后依次寻找下一个(结束时前最早的)不重叠的区域,这样就得到了数量最多的构成不重叠的区域的数量,再用总数量减去最大不重叠区域的数量,就得到了最少的会引起重叠的区域的数量 ...