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了一发 最 ...
随机推荐
- Node 开启
cmd //进入命令行 D: //指定磁盘 cd 文件路径 //指定路径 node 文件名.js //执行文件 增补: Node执行js文件自动嵌套 (functio ...
- linux学习之centos(三):mysql数据库的安装和配置
前言:mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库, ...
- 微信小程序的界面下拉刷新
小程序的下拉刷新的值设置:需要设置app.json的window中 "navigationBarTextStyle":true
- LODOP、C-LODOP注册号的区别
LODOP是一款免费的web打印控件,预览打印后无水印,是免费的,直接打印会在纸张下方有个水印“本页由XXX试用版输出”,通常商用打印较多,常用直接打印,这种时候可以购买注册号去水印. LODOP注册 ...
- IPv6应用普及,任重而道远
导读 2018年初开始,全国尤其是运营商网络开启了IPv6改造的大幕,很多企业都从工信部领到了军令状,要将IPv6改造工作彻底落实下去.现在2018年已接近尾声,回头看看实际部署情况如何. IPv6涉 ...
- std::string 字符串替换
std::string 没有原生的字符串替换函数,需要自己来完成 string& replace_str(string& str, const string& to_repla ...
- 自学Zabbix3.12.6-动作Action-Escalations配置
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 3.12.6 自学Zabbix3.12.6-动作Action-Escalations配置 1. 概 ...
- wireshark配合jmeter测试webservice接口
1.首先,获取本地和接口的ip,以便设置过滤 2.wireshark设置过滤 ip.dst==192.168.0.101 and ip.src==61.147.124.120 and http 3.执 ...
- java反射出字段信息和值
/** * */ package test; import java.lang.reflect.Field; import java.lang.reflect.Modifier; /** * @aut ...
- Raspbian首次安装后无法使用SSH链接
使用Putty连接树莓派,出现Network Error:Connection Refused 新版的Raspbian系统默认禁用了SSH. 解决方法:在/boot分区创建名为"ssh&qu ...