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每个顶点看成一个图.将最小权值 ...
随机推荐
- CentOS7.4安装部署openstack [Liberty版] (一)
一.OpenStack简介 OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目. OpenStack是一个 ...
- vuejs实现瀑布流布局(一)
一直以来,习惯了jquery的DOM操作方式,突然间,开始学习使用vuejs,很多时候,操作DOM观念总是转换不过来,虽然也能实现各种效果,但是总有点不伦不类的. 就类似于最近在做的瀑布流布局,正常的 ...
- JDK-1.8-CopyOnWriteSet
CopyOnWriteSet 看了下CopyOnWriteSet源码,底层使用的是CopyOnWriteList,根据底层的实现,每次读取都是N的复杂度.每次写也是N的复杂度.有个代码可以看一下, 来 ...
- Python学习URL
Python 与 Excel 不得不说的事 http://www.open-open.com/lib/view/open1472701496085.html
- java判断字符串中是否包含中文 过滤中文
package com.test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test ...
- mybatis一(常用配置信息和获取插入后id)
<!--配置文件--><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE conf ...
- maven项目里的mapper不被加载,解析
出现这个错误是因为maven加载配置文件是从resource里加载的,所以要配置一下
- 安装Ubuntu
win7和vista如何登录administrator账户 首先关闭UAC(如何关闭Windows7 UAC?二种方法进入“UAC设置界面”: 方法1:开始→控制面板→查看方式:大图标→操作中心→更改 ...
- vector 内存释放相关
在使用vector 存储char 类型时 new的数据 在vecto 执行rerase的时候或者 单项执行clear 内存是不能被释放的. 比如有下面的一个测试: class SnapshotInfo ...
- copyOnWriteArray 并发包下的不安全(数组)集合
copyOnWriteArray 记录一下 package java.util.concurrent;//你没有看错,是这个包 private transient volatile Object[] ...