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. [Unity动画]01.HasExitTime & ApplyRootMotion

    参考链接: https://www.cnblogs.com/hammerc/p/4828774.html 资源下载: https://assetstore.unity.com/packages/ess ...

  2. The difference between Spring Tool Suite and Spring IDE

    The difference between Spring Tool Suite and Spring IDE Spring Tool Suite和Spring IDE的区别: http://down ...

  3. javafx自动补全

    public boolean showUpload2HomeDialog(final OperationInfo initDataInfo) { boolean result = false; try ...

  4. 8.Appium的基本使用-3(安装JDK、android-sdk)

    1.下载安装JDK :https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html安装教程参 ...

  5. 廖雪峰老师Python3教程练习整理

    1.定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax2 + bx + c = 0的两个解 # -*- coding: utf-8 -*-import mathde ...

  6. Asp.Net前台调用后台变量

    1.Asp.Net中几种相似的标记符号: < %=...%>< %#... %>< % %>< %@ %>解释及用法 答: < %#... %&g ...

  7. USB之HID类Set_Report Request[调试手记1]

    请翻开<Device Class Definition for Human Interface Devices (HID) Version 1.11 >7.2.2 Set_Report R ...

  8. 浅析USB HID ReportDesc (HID报告描述符)

    在USB中,USB Host是通过各种描述符来识别识别设备的,一般在设备枚举的过程将会获取有设备描述符/配置描述符/接口描述符/端点描述符/字符串描述符等 现在我们来介绍一下HID ReportDes ...

  9. js把mysql传过来的时间格式化为:0000-00-00 00:00:00

    mysql传到前端的默认时间:2018-01-06T17:32:23+08:00 格式化的js代码 var time = '2018-01-06T17:32:23+08:00' var span = ...

  10. VUE.js全局变量的定义

    模块化之后,想用js全局变量,遇到点困难.搜索资料后搞定,大概2个步骤: 1.定义一个vue模块,const定义变量,并用export对外暴露. Globle.vue <script> / ...