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\)次交换依次进行, ...
随机推荐
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 19-Python3 函数
def area(width,heigh): return width*heigh def print_wecome(name): print('welcome',name) print('Runoo ...
- [LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- leetcode 93 复原IP地址
IP地址,分成四段,每段是0-255,按照每段的长度分别为1,2,3下一段长度分别1,2,3再下一段......进行递归遍历,能满足条件的假如res中.比较难想到的就是假如有一段是三位的010是不符合 ...
- 从零开始一起学习SLAM | 神奇的单应矩阵
小白最近在看文献时总是碰到一个奇怪的词叫“homography matrix”,查看了翻译,一般都称作“单应矩阵”,更迷糊了.正所谓:“每个字都认识,连在一块却不认识”就是小白的内心独白.查了一下书上 ...
- js重定向跳转页面
重定向方式: 1> window.location ='www.baidu.com'; window.location='/'; window.location='/logout/'; ...
- 笔记 : Ubuntu部署LNMP环境
一.准备与安装 1. 安装PHP7.1 #添加php源 :~$ sudo add-apt-repository ppa:ondrej/php #更新apt数据,载入php源数据 :~$ sudo ap ...
- JAVA多线程常用例子
详情见该页面ttps://www.cnblogs.com/pureEve/p/6524366.html
- c# 在mongo中查询经纬度范围
#region 索引 //IndexKeysDocument doc = new IndexKeysDocument();//新建索引 //2d 平面坐标索引,适用于基于平面的坐标计算.也支持球面距离 ...
- XML小结
一.因为某些字符在xml格式中,具有特殊意义,所以当我们需要使用它本身的意思的时候,就要用其他东西来代替它,否则会产生错误 < < less than > > greater ...