poj3723_Conscription
|
Conscription
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. 1 ≤ N, M ≤ 10000 Output For each test case output the answer in a single line.
Sample Input 2 5 5 8 Sample Output 71071 Source POJ Monthly Contest – 2009.04.05, windy7926778
|
[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的更多相关文章
随机推荐
- Error opening terminal: xterm-256color
在使用gdb调试linux内核时,提示如下错误: arm-none-linux-gnueabi-gdb --tui vmlinux Error opening terminal: xterm-256c ...
- nvidia-docker2配置与NVIDIA驱动安装
要运行高版本的GPU版TensorFlow,需要更新宿主机的显卡驱动(本文以NVIDIA390为例) 一.更新驱动 禁用nouveau驱动: 添加/etc/modprobe.d/blacklist.c ...
- Error-MVC: 未能找到路径“D:\\DsWeb\DS.Web\dist\bin\roslyn\csc.exe”的一部分。
ylbtech-Error-MVC: 未能找到路径“D:\\DsWeb\DS.Web\dist\bin\roslyn\csc.exe”的一部分. 1.返回顶部 1, “/”应用程序中的服务器错误. 未 ...
- Git结合tar自动打升级包
背景最近在看Git,那么看了之后就需要用Git来解决一些工作中遇到的问题,学了不能用在工作中,等于白学. 这次遇到的问题是打包升级的问题,我们公司目前还处于最原始的手工打更新包的状况,每次打包都要找开 ...
- 转: ffmpeg循环推流方法
from: https://blog.csdn.net/weiyuefei/article/details/64125208 ffmpeg循环推流方法 You should be able to u ...
- angular 2 - 005 路由实现机制
angular2的路由是不是很神奇, url发生了变化却没有看到有任何请求发出? 1. hash模式 url类似 http://localhost:4200/#/task-list,跳转到路由页面再刷 ...
- 运行Keras版本的Faster R-CNN(1)
Keras版本的Faster R-CNN源码下载地址:https://github.com/yhenon/keras-frcnn下载以后,用PyCharm打开(前提是已经安装了Tensorflow-g ...
- iOS实现 webView loadHTMLString加载外部css、js样式
记录一下. webview(或wk)用 loadHTMLString加载内容时 ,如果只是单纯的html内容,样式等都写在内部,直接设置baseURL为nil即可. 不过当html里包含外部样式或调用 ...
- Effective Java 第三版——80. EXECUTORS, TASKS, STREAMS 优于线程
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- Atitit 3种类型的公司:运营驱动型;产品驱动型; 技术驱动型。
Atitit 3种类型的公司:运营驱动型:产品驱动型: 技术驱动型. 领导驱动,产品驱动,运营驱动还是工程师驱动 3种类型的公司: 一种是运营驱动型: 一种是产品驱动型: 一种技术驱动型. 运营驱动 ...