bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823
讨厌的形状就是四联通图
且
左右各连一个方块
那么破坏所有满足条件的四联通就好了

按上图方式染色之后,任意满足要求的四联通块一定可以是
黑色-->紫左-->紫右-->白色
只要破坏三个箭头中的一个即可
所以可以构建最小割模型
1、源点向黑色格连流量为格子代价的边
2、黑色格向相邻的紫色格连inf边
3、与黑色格相邻的紫色格向与白色格相邻的紫色格连 流量 为 两个紫色格较小代价 的边
4、与白色相邻的紫色格向白色格连inf边
5、白色格向汇点连流量为格子代价的边
染完之后长这样:

注意:
不要在枚举紫色格子的过程中连源点汇点的边
这样会导致连重边

比如这样黑色格子就会与源点有重边,两个紫色格子各贡献了一条边
但实际我们只能用一条边
所以可以标记哪些格子与源点、汇点有边,最后再连
(再次吐槽一次bzoj的题面~~)
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue> using namespace std; typedef long long LL; #define N 100002
#define M 1400001 const int inf=2e9; map<LL,int>mp; int n,m,k;
int xi[N],yi[N],zi[N]; int front[N],nxt[M<<],to[M<<],cap[M<<],tot=;
int lev[N],cur[N];
int src,decc;
queue<int>q; bool uses[N],uset[N]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} LL turn(int i,int j)
{
return 1LL*(i-)*m+j;
} void init()
{
read(m); read(n); read(k);
int y,x;
for(int i=;i<=k;++i)
{
read(yi[i]); read(xi[i]); read(zi[i]);
mp[turn(xi[i],yi[i])]=i;
}
decc=k+;
} void add(int u,int v,int val)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; cap[tot]=val;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; cap[tot]=;
// printf("%d %d %d\n",u,v,val);
} void Add(int x,int l,int r,int y)
{
if(y%== || !(y%))
{
uses[x]=true;
add(x,l,inf);
}
else
{
uset[x]=true;
add(r,x,inf);
}
} void build()
{
int x,y;
int tmp,l,r;
int l1,l2,l3,r1,r2,r3;
for(int i=;i<=k;++i)
{
x=xi[i]; y=yi[i];
if(y==m) continue;
if(((x&) && y%==) || (!(x&) && y%==))
{
tmp=mp[turn(x,y+)];
if(!tmp) continue;
}
else continue;
if(x>) l1=mp[turn(x-,y)]; else l1=;
if(x<n) l2=mp[turn(x+,y)]; else l2=;
if(y>) l3=mp[turn(x,y-)]; else l3=;
if(!(l1||l2||l3)) continue;
if(x>) r1=mp[turn(x-,y+)]; else r1=;
if(x<n) r2=mp[turn(x+,y+)]; else r2=;
if(y<n-) r3=mp[turn(x,y+)]; else r3=;
if(!(r1||r2||r3)) continue;
l=i; r=tmp;
if(y%==) swap(l,r);
add(l,r,min(zi[l],zi[r]));
if(l1) Add(l1,l,r,yi[l1]);
if(l2) Add(l2,l,r,yi[l2]);
if(l3) Add(l3,l,r,yi[l3]);
if(r1) Add(r1,l,r,yi[r1]);
if(r2) Add(r2,l,r,yi[r2]);
if(r3) Add(r3,l,r,yi[r3]);
}
for(int i=;i<=k;++i)
if(uses[i]) add(src,i,zi[i]);
for(int i=;i<=k;++i)
if(uset[i]) add(i,decc,zi[i]);
} bool bfs()
{
while(!q.empty()) q.pop();
for(int i=src;i<=decc;++i) lev[i]=-,cur[i]=front[i];
lev[src]=;
q.push(src);
int now,t;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=front[now];i;i=nxt[i])
{
t=to[i];
if(lev[t]==- && cap[i])
{
lev[t]=lev[now]+;
if(t==decc) return true;
q.push(t);
}
}
}
return false;
} int dinic(int now,int flow)
{
if(now==decc) return flow;
int rest=,delta,t;
for(int &i=cur[now];i;i=nxt[i])
{
t=to[i];
if(lev[t]>lev[now] && cap[i])
{
delta=dinic(t,min(flow-rest,cap[i]));
if(delta)
{
cap[i]-=delta;
cap[i^]+=delta;
rest+=delta;
if(rest==flow) break;
}
}
}
if(rest!=flow) lev[now]=-;
return rest;
} void solve()
{
int ans=;
while(bfs()) ans+=dinic(src,inf);
printf("%d",ans);
} int main()
{
freopen("data.in","r",stdin);
freopen("my.out","w",stdout);
init();
build();
solve();
}
bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块的更多相关文章
- bzoj4823: [Cqoi2017]老C的方块(最小割)
4823: [Cqoi2017]老C的方块 题目:传送门 题解: 毒瘤题ORZ.... 太菜了看出来是最小割啥边都不会建...狂%大佬强强强 黑白染色?不!是四个色一起染,四层图跑最小割... 很 ...
- bzoj千题计划196:bzoj4826: [Hnoi2017]影魔
http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...
- bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...
- bzoj千题计划251:bzoj3672: [Noi2014]购票
http://www.lydsy.com/JudgeOnline/problem.php?id=3672 法一:线段树维护可持久化单调队列维护凸包 斜率优化DP 设dp[i] 表示i号点到根节点的最少 ...
- bzoj千题计划177:bzoj1858: [Scoi2010]序列操作
http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...
- bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)
https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...
- bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)
https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...
- bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹
http://www.lydsy.com/JudgeOnline/problem.php?id=2244 每枚导弹成功拦截的概率 = 包含它的最长上升子序列个数/最长上升子序列总个数 pre_len ...
- bzoj千题计划278:bzoj4590: [Shoi2015]自动刷题机
http://www.lydsy.com/JudgeOnline/problem.php?id=4590 二分 这么道水题 没long long WA了两发,没判-1WA了一发,二分写错WA了一发 最 ...
随机推荐
- Activiti reassign task to another user
//早先胡乱尝试的其他方法,可能对于以后深入学习Activiti有些用处. //taskService.delegateTask(taskId, receiveUserId); //taskServi ...
- No input file specified ci
1. php.ini(/etc/php5/cgi/php.ini)的配置中这两项cgi.fix_pathinfo=1 (这个是自己添加的)
- kali linux升级
自己使用的是2017.2 版本的kali linux 想着升级一下 里面的包 比如msf 等 但是执行 msfupdate时提示 root@kali201702:~# msfupdate msfupd ...
- C++与C的区别
在最开始C++只是C加上了一些面向对象的特性,C++最初的名字为C with Classes.后来C++又提出了一些不同于Class的特性:Exceptions(异常).templates(模板).S ...
- CSS 范围选择器(自编)
选择第一个到第六个li元素ul li:nth-child(n+3):not(:nth-child(n+6)){} 选择第二个到最后一个ul li:nth-child(2)~li{} 选择除了第一个和最 ...
- 金蝶特性配置(超级BOM)
特性配置 特性物料 物料和特性的对应关系 自动新增物料的系统设置 特性物料自动新增 自动新增的特性配置方案 相关表 ICPlan_CharacteristicEntry
- jquery Ajax get()/post()
get()/post()是通過http get/post向服務器請求數據的. http get vs post: get:向指定資源獲取數據 post項指定資源提交數據. get是向遠程服務器的獲取數 ...
- Bootstrap自动定位浮标
前面的话 Affix 插件主要功能就是通过插件给某个元素(需要固定的元素)添加或删除position:fixed,实现元素在浏览器窗口的粘性固定效果.本文将详细介绍Bootstrap自动定位浮标 基本 ...
- 闭包自由变量引用对象的问题 http://bbs.pythontab.com/thread-4266-1-1.html
- codeforces624A
Save Luke CodeForces - 624A Luke Skywalker got locked up in a rubbish shredder between two presses. ...