Conscription
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12393   Accepted: 4350

Description

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

刚开始差点理解错意思,原来是男女两种人,windy必须要买掉所有人是(N+M)*10000,然后每两个人认识会给windy减少d的花费,kruskal中按照从大到小排序即可。

另外也可以把-d存进去,那样kruskal中就不用修改了。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define for(i,x,n) for(int i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAX_N 50005 using namespace std; struct edge{int u,v,cost;};
edge es[MAX_N];
int V,E; int par[MAX_N];
int depth[MAX_N];//记录每个节点下面到位深度 void init(int n)//初始化
{for(i,,n) {par[i]=i;depth[i]=;}}
int findf(int t)//寻找根节点
{ return t==par[t] ? t:par[t]=findf(par[t]);}
bool same(int x,int y)//是否在同一组内
{return findf(x)==findf(y);}
void unite(int t1,int t2){//成为一组
int f1=findf(t1);
int f2=findf(t2);
if(f1==f2){
return ;
}
if(depth[f1]<depth[f2]){
par[f1]=f2;
}else{
par[f2]=f1;
if(depth[f1]==depth[f2]){
depth[f1]++;//记录深度
}
}
} bool comp(edge a,edge b){
return a.cost>b.cost;
} int kruskal(){
sort(es,es+E,comp);
init(V);
int res=;
for(i,,E){
edge e=es[i];
if(!same(e.u,e.v)){
unite(e.u,e.v);
res+=e.cost;
}
}
return res;
} int main()
{
//freopen("data.txt", "r", stdin);
//freopen("data.out", "w", stdout);
int N,M,R;
int x,y,d;
int n;
scanf("%d",&n);
while(n--){
scanf("%d %d %d",&N,&M,&R);
V=N+M;
E=R;
for(i,,R){
scanf("%d %d %d",&x,&y,&d);
es[i].u=x;
es[i].v=y+N;
es[i].cost=d;
}
int res=kruskal();
printf("%d\n",(N+M)*-res); }
//fclose(stdin);
//fclose(stdout);
return ;
}

poj3723_Conscription的更多相关文章

随机推荐

  1. RESTEASY ,从学会使用到了解原理。

    转载本文需注明出处:EAII企业架构创新研究院,违者必究.如需加入微信群参与微课堂.架构设计与讨论直播请直接回复公众号:“EAII企业架构创新研究院”.(微信号:eaworld) 1,背景知识; 1. ...

  2. tfs2015 生成与发布 配置

    先来看一张微软官方的自动生成与发布架构图,以便了解很多概念间的关系 1.安装好TFS2015(可以参考TFS2010的安装过程,尤其是账号权限相关),我自己是从TFS2010一路升级上来的(TFS20 ...

  3. MySQL匹配指定字符串的查询

    MySQL匹配指定字符串的查询 使用正则表达式查询时,正则表达式可以匹配字符串.当表中的记录包含这个字符串时,就可以将该记录查询出来.如果指定多个字符串时,需要用“|”符号隔开,只要匹配这些字符串中的 ...

  4. APB协议

    https://wenku.baidu.com/view/2663f629ef06eff9aef8941ea76e58fafab04592.html https://www.cnblogs.com/l ...

  5. java socket编程中backlog的含义(zz)

    使用Java.NET.ServerSocket能够方便的创建一个服务端套接字,这个类的构造函数有一个参数backlog.下面这段代码,在本机的8888端口上建立了一个套接字,backlog设置为5. ...

  6. 柳青(Jean)英文演讲集合

    1.Didi Chuxing's Jean Liu on The Future of Cities  https://www.youtube.com/watch?v=G9uPGoN0dvQ 2.Did ...

  7. JS获取当前日期、比较日期大小

    //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = "-& ...

  8. 关于4A(统一安全管理平台)系统的理解

    1. 4A系统的需求分析 近年来企业用户的业务系统发展十分迅速,内部的系统数和用户数不断增加,网络规模迅速扩大,在应用扩展的同时,各业务系统的安全管理工作相对滞后,无法满足企业发展的长期要求. 各系统 ...

  9. 自建k8s集群日志采集到阿里云日志服务

    自建k8s集群 的master 节点安装 logtail 采集工具 wget http://logtail-release-cn-hangzhou.oss-cn-hangzhou.aliyuncs.c ...

  10. Linux Crontab及使用salt进行管理

    一.引言: 最近无意之间看到salt有一个cron的模块,今天就在这里介绍linux crontab以及通过salt的cron对crontab的管理. 二.Linux crontab的介绍: cron ...