bzoj5421:收藏家
贴一张图

关于对问题的转化:
当两个人交换收藏品时,显然我们进行这个操作是为了得到更优解。
那么一个收藏品是有用的,另一个被换走的收藏品可以当做直接扔掉了。
所以只要保留一个就可以了。
注意边数、点数要开够
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 20010
int min(int a,int b){return a<b?a:b;}
int n,m,u,S,T,val[N],d[N],cur[N],fir[N]; bool vis[N];
int cnt,hd[N],nxt[N<<],ed[N],poi[N<<];
int cap[N<<],flow[N<<],ans;
queue <int> h;
void adde(int x,int y,int v1,int v2){
nxt[ed[x]]=++cnt; hd[x]=hd[x]?hd[x]:cnt;
ed[x]=cnt; poi[cnt]=y; flow[cnt]=v1; cap[cnt]=v2;
}
#define to poi[i]
bool bfs(){
memset(vis,,sizeof(vis));
h.push(S); vis[S]=; d[S]=;
while(!h.empty()){
int x=h.front(); h.pop();
for(int i=hd[x];i;i=nxt[i])
if(!vis[to]&&cap[i]>flow[i]){
vis[to]=; d[to]=d[x]+;
h.push(to);
}
}return vis[T];
}
int dinic(int x,int a){
if(!a||x==T) return a;
int F=,f;
for(int &i=cur[x];i&&a;i=nxt[i])
if(d[to]==d[x]+&&(f=dinic(to,min(a,cap[i]-flow[i])))>){
F+=f; flow[i]+=f;
a-=f; flow[i^]-=f;
}
return F;
}
void nect(int x,int y,int v){adde(x,y,,v);adde(y,x,,);}//一条边以及它的反向边
int main(){
int TuT;scanf("%d",&TuT);
while(TuT--){
memset(hd,,sizeof(hd));
memset(nxt,,sizeof(nxt));
memset(ed,,sizeof(ed));cnt=;
scanf("%d%d",&n,&m);
S=;T=u=;ans=;
for(int i=;i<=n;++i){
scanf("%d",&val[i]);
nect(S,++u,);fir[i]=u;
}
for(int i=,q1,q2;i<=m;++i){
scanf("%d%d",&q1,&q2);
nect(fir[q1],++u,val[q1]);fir[q1]=u;
nect(fir[q2],++u,val[q2]);fir[q2]=u;
nect(fir[q1],fir[q2],);
nect(fir[q2],fir[q1],);
}nect(fir[],T,val[]);
while(bfs()){//普通dinic
for(int i=;i<=u;++i) cur[i]=hd[i];
ans+=dinic(S,2e9);
}printf("%d\n",ans);
}return ;
}
bzoj5421:收藏家的更多相关文章
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 2014年8月25日,收藏家和杀手——面向对象的C++和C(一)
近期事情特别多,睡眠也都非常晚,有点精神和身体混乱的感觉,所以想写写技术分析文章.让两者的我都调整一下.这篇技术分析文章是一直想写的,当前仅仅是开篇,有感觉的时候就写写,属于拼凑而成,兴许的篇章没有时 ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- [USACO16OPEN]钻石收藏家Diamond Collector
由于相差不超过k才可以放在一起,要判断不超过k这个条件,显然我们需要排序 首先我们需要一个f数组,f[i]意义看代码开头注释, 假设我们可以选择的某一个区间是a[l]~a[r](已排序且最优(最长的意 ...
- 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...
- BZOJ 5421: 收藏家
传送门 直接搞很复杂,考虑转化问题 题目只要求第1个人最多能获得的物品数量 所以如果一种物品拥有多个和一个是没区别的 那么考虑每种物品对第1个人怎样贡献 显然要经过一些交换最终到达第一个人那里 发现很 ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- 洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector
题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...
- YbtOJ-大收藏家【分层图,最大流】
正题 题目链接:https://www.ybtoj.com.cn/contest/117/problem/2 题目大意 \(n\)个人,每人有\(a_i\)个属于自己的物品.\(m\)次交换依次进行, ...
随机推荐
- ssh 的认证原理
SSH:Secure Shell,是一种网络安全协议,主要用于登录远程计算机的加密过程. 登录方式主要有两种: 1.基于用户密码的登录方式: 加密原理: 当服务器知道用户请求登录时,服务器会把 ...
- JS发送短信验证码
<div> <input type="tel" id="mobile" name="mobile" placeholder ...
- win7 x64安装TensorFlow
在windows下安装的TensorFlow做学习研究之用,如果要进行技术,请看相关博文:CentOS7安装TensorFlow 1.安装Pytho3.5 首先到Anaconda网站去下载Window ...
- CentOS6.5 安装Kafka集群
1.安装zookeeper 参考文档:http://www.cnblogs.com/hunttown/p/5452138.html 2.下载:https://www.apache.org/dyn/cl ...
- jmeter二次开发----Loadrunner或Jmeter发送邮件报告
Loadrunner支持Java Vuser,而Jmeter本身就是基于Java开发的,所以两者都可以通过JMail组件实现邮件发送.本人使用的是mail-1.4.7.jar,可以通过下载获得:htt ...
- iOS 新浪微博-5.2 首页微博列表_转发微博/工具栏
继续于上一篇,还是做首页的功能,这一篇把剩下的首页继续完善. 看看上面的图片,分析: 1.转发微博里面的内容,和原创微博是一样的,由文字+配图组成.这应该放在一个UIView里处理. 2.工具栏也当成 ...
- 关于事件循环机制event loop
setTimeout(()=> { console.log('settimeout') },100) console.log('开始') console.log('结束') new Promis ...
- cocos2d-x JS 开启远程代码调试
为了方便服务端与测试部门的工作需要,把客户端的写的程序可以在其他电脑上运行,方便他人工作与测试. 下面是cocos2d-x JS WebStorm 的设置方法.
- 注解(Annotation)
注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是 ...
- 安装redis脚本
#!/bin/bash#Function: Install redis#Author: DengYong#Date: 20151126 cd /root/packagewget http://down ...