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:收藏家的更多相关文章

  1. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  2. 2014年8月25日,收藏家和杀手——面向对象的C++和C(一)

    近期事情特别多,睡眠也都非常晚,有点精神和身体混乱的感觉,所以想写写技术分析文章.让两者的我都调整一下.这篇技术分析文章是一直想写的,当前仅仅是开篇,有感觉的时候就写写,属于拼凑而成,兴许的篇章没有时 ...

  3. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  4. [USACO16OPEN]钻石收藏家Diamond Collector

    由于相差不超过k才可以放在一起,要判断不超过k这个条件,显然我们需要排序 首先我们需要一个f数组,f[i]意义看代码开头注释, 假设我们可以选择的某一个区间是a[l]~a[r](已排序且最优(最长的意 ...

  5. 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

        解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...

  6. BZOJ 5421: 收藏家

    传送门 直接搞很复杂,考虑转化问题 题目只要求第1个人最多能获得的物品数量 所以如果一种物品拥有多个和一个是没区别的 那么考虑每种物品对第1个人怎样贡献 显然要经过一些交换最终到达第一个人那里 发现很 ...

  7. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...

  8. 洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...

  9. YbtOJ-大收藏家【分层图,最大流】

    正题 题目链接:https://www.ybtoj.com.cn/contest/117/problem/2 题目大意 \(n\)个人,每人有\(a_i\)个属于自己的物品.\(m\)次交换依次进行, ...

随机推荐

  1. Struct2中自定义的Filter无效

    解决办法,把自定义的Filter配置放在struct2前 <?xml version="1.0" encoding="UTF-8"?> <we ...

  2. windows 邮槽mailslot 在服务程序内建立后客户端无权限访问(GetLastError() == 5)的问题

    邮槽创建在服务程序内,可以创建成功, 但外部客户端连接时 m_hMailslot = CreateFile("\\\\.\\mailslot\\zdpMailslot",GENER ...

  3. 亲爱的,我是一条Linux运维技术学习路径呀。

    根据我的经验,人在年轻时,最头疼的一件事就是决定自己这一生要做什么.在这方面,我倒没有什么具体的建议:干什么都可以,但最好不要写小说,这是和我抢饭碗.总而言之,干什么都是好的:但要干出个样子来,这才是 ...

  4. 带下拉列表的TextBox

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. cocos2dx 3.x(for 循环让精灵从中间往上下两边排列)

    最近很多游戏都喜欢房卡类的游戏,就是创建房间时(),选择玩法与规则,今天耗费2小时处理这个数学问题:例如选择规则两条,则背景框中间显示两条规则,若选择三条,则背景框中间显示三条规则与玩法,依次从中间往 ...

  6. EF There is already an open DataReader associated with this Command

    捕捉到 System.InvalidOperationException _HResult=-2146233079 _message=意外的连接状态.在使用包装提供程序时,请确保在已包装的 DbCon ...

  7. Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  8. Docker 在转发端口时的这个错误Error starting userland proxy: mkdir /port/tcp:0.0.0.0:3306:tcp:172.17.0.2:3306: input/output error.

    from:https://www.v2ex.com/amp/t/463719 系统环境是 Windows 10 Pro,Docker 版本 18.03.1-ce,电脑开机之后第一次运行 docker ...

  9. Django 一对一,一对多,多对多 操作、常用方法

    几对几的模型结构的使用场景为:一般根据业务需求,同一业务,需要向相关联的多表插入删除数据时,会用到. 一对一: 创建一个用户表 class Users(models.Model): username ...

  10. python subprocess中ssh命令的特殊性

    今天尝试使用python 的 subprocess 模块 使用类似如下语句: p=subprocess.Popen(['ssh','root@localhost'],stdout=subprocess ...