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了一发 最 ...
随机推荐
- Laravel route ---- resoure
Laravel 路由--资源路由 Route::resource('blog', 'BlogController'); 上面代码将等同于: Route::get('/blog', 'BlogContr ...
- Django的datetime.timedelta类(Django编程-2)
datetime.timedelta对象代表两个时间之间的时间差,两个date或datetime对象相减就可以返回一个timedelta对象. 如果有人问你昨天是几号,这个很容易就回答出来了.但是如果 ...
- BZOJ5317 JSOI2018部落战争(凸包)
即询问凸包是否有交.这显然可以直接求半平面交,但是复杂度O(q(n+m)),且没有什么优化空间. 更直接地表示,即相当于询问是否存在点a∈A,b∈B,使得a+d=b.移项,得到d=b-a.可以发现等式 ...
- java 前台使用枚举方法(二)
最近发现,前台jsp使用枚举,有一个更方便的方法. 首先 枚举类的封装大家看一下:http://blog.csdn.net/hanjun0612/article/details/72845960 然后 ...
- POI 生成excel(大数据量) SXSSF
使用POI 的SXSSF (Streaming Usermodel API)生成较大的excel,同时开启压缩 import junit.framework.Assert; import org.ap ...
- Mysterious Bacteria LightOJ - 1220
题意: 给出一个数x 求 x = bp 的p的最大值 解析: 算术基本定理 分解质因数 任何一个数x都可以表示为 x == p1a1 * p2a2 * ````` * pnan 即 b ...
- Application Server not specified
IDEA使用tomcat启动web项目,配置页面报错Application Server not specified: 那是因为没有配置tomcat,只要配置一下就好了:
- JNative 传递参数bug
下载JNative网址:http://sourceforge.net/projects/jnative/files/jnative/ 下载JNative版本:JNative_1.4RC3_bin.zi ...
- Java8的Stream流(一) --- 基础用法
Java8中的Stream Stream使用一种类似用SQL语句从数据库查询数据的直观方式来提供一种对Java集合运算和表达的高阶抽象. Stream的特性及优点: 无存储. Stream不是一种数据 ...
- c c++ 混合编译
单个源文件生成可执行程序 下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: 1 2 3 4 5 6 7 8 9 /* helloworld.cpp */ # ...