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的方块的更多相关文章

  1. bzoj4823: [Cqoi2017]老C的方块(最小割)

    4823: [Cqoi2017]老C的方块 题目:传送门 题解: 毒瘤题ORZ.... 太菜了看出来是最小割啥边都不会建...狂%大佬强强强   黑白染色?不!是四个色一起染,四层图跑最小割... 很 ...

  2. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  3. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

  4. bzoj千题计划251:bzoj3672: [Noi2014]购票

    http://www.lydsy.com/JudgeOnline/problem.php?id=3672 法一:线段树维护可持久化单调队列维护凸包 斜率优化DP 设dp[i] 表示i号点到根节点的最少 ...

  5. bzoj千题计划177:bzoj1858: [Scoi2010]序列操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...

  6. bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)

    https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...

  7. bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...

  8. bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹

    http://www.lydsy.com/JudgeOnline/problem.php?id=2244 每枚导弹成功拦截的概率 = 包含它的最长上升子序列个数/最长上升子序列总个数 pre_len ...

  9. bzoj千题计划278:bzoj4590: [Shoi2015]自动刷题机

    http://www.lydsy.com/JudgeOnline/problem.php?id=4590 二分 这么道水题 没long long WA了两发,没判-1WA了一发,二分写错WA了一发 最 ...

随机推荐

  1. 传输层中的协议 TCP & UDP

    面向连接的TCP协议 “面向连接”就是在正式通信前必须要与对方建立起连接.比如你给别人打电话,必须等线路接通了.对方拿起话筒才能相互通话.TCP(Transmission Control Protoc ...

  2. LoadRunner12 Java Vuser API语法举例

    // 检查点 web.reg_find("Text=\"retCode\":\"0000\"",new String[]{"FAI ...

  3. Word2010设置题注和交叉引用方法

    设置题注 点击图片-->右键-->插入题注-->新建标签:“图”-->选择新建标签“图”-->修改“编号”-->勾选包含章节号-->设置章节起始样式:标题2- ...

  4. Bootstrap洼地

    前面的话 这是一个轻量.灵活的组件,它能延伸至整个浏览器视口来展示网站上的关键内容.本文将详细介绍Bootstrap洼地 概述 洼地(Well)样式的效果和巨幕jumbotron样式类似,不同点是we ...

  5. 配置dataimport时候 如果css样式有问题 要修改index和admin的版本号

  6. pgm13

    这部分开始,我们将讨论 learning 相关的内容.PGM 为 frequentist 与 Bayesian 系的 model 提供了同一种语言,对前者来说 learning 就是确定一种对“未知但 ...

  7. BZOJ4399魔法少女LJJ——线段树合并+并查集

    题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...

  8. JS通过键盘点击事件实现div移动

    页面内容:文本框模拟键盘点击   div元素实现移动: <body> <textarea id="myarea"></textarea> < ...

  9. Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)

    题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...

  10. 2015 HIAST Collegiate Programming Contest D

    You have been out of Syria for a long time, and you recently decided to come back. You remember that ...