bzoj 4823: [Cqoi2017]老C的方块 [最小割]
4823: [Cqoi2017]老C的方块
题意:
鬼畜方块游戏不解释...
有些特殊边,有些四个方块组成的图形,方块有代价,删掉一些方块使得没有图形,最小化代价。
比较明显的最小割,一个图形中必须删掉一个方块。
我的想法是方块拆点然后用INF连起来。
但是你不能随便连啊,否则可能会出现一些原来没有的限制。
要找到一个连边的顺序!也就是如何分层
画一画发现是可以做到的,然后建图就行了。
我是一层一层建的...
然后一开始忘记考虑一种图形WA了两次...
总共花了3个多小时...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5+5, M = 2e6+5, INF = 1e9+5;
inline int read() {
char c=getchar(); int x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
int m, n, k, s, t;
struct meow{int x, y, w;} a[N];
map<int, int> g[100005];
struct edge{int v, ne, c, f;} e[M];
int cnt=1, h[N];
inline void ins(int u, int v, int c) {
e[++cnt] = (edge){v, h[u], c, 0}; h[u] = cnt;
e[++cnt] = (edge){u, h[v], 0, 0}; h[v] = cnt;
}
int start(int x, int y) {
if((x-2)%4 == 0) {
if(!(y&1)) return 1;
else return 0;
}
if((x+1)%4 == 0) {
if(y&1) return 2;
else return 0;
}
return 0;
}
inline void link(int u, int v) { ins(u+k, v, INF); }
int t1[N], t2[N];
void build() {
s = 0; t = k+k+1;
for(int i=1; i<=k; i++) ins(i, i+k, a[i].w);
int v;
int *mark = t1, *mark2 = t2;
for(int i=1; i<=k; i++) {
int x = a[i].x, y = a[i].y, p = start(x, y);
if(!p) continue;
ins(s, i, INF); //printf("start (%d, %d) %d\n", x, y, p);
if(p == 1) {
if(g[x].count(y+1)) v = g[x][y+1], link(i, v), mark[v] = 1;
if(g[x].count(y-1)) v = g[x][y-1], link(i, v), mark[v] = 1;
if(g[x+1].count(y)) v = g[x+1][y], link(i, v), mark[v] = 2;
} else {
if(g[x].count(y+1)) v = g[x][y+1], link(i, v), mark[v] = 2;
if(g[x].count(y-1)) v = g[x][y-1], link(i, v), mark[v] = 2;
if(g[x-1].count(y)) v = g[x-1][y], link(i, v), mark[v] = 1;
}
}
swap(mark, mark2);
for(int i=1; i<=k; i++) if(mark2[i]) {
int x = a[i].x, y = a[i].y, p = mark2[i]; mark2[i] = 0;
if(p == 1 && g[x-1].count(y)) v = g[x-1][y], link(i, v), mark[v] = 1;
if(p == 2 && g[x+1].count(y)) v = g[x+1][y], link(i, v), mark[v] = 2;
}
swap(mark, mark2);
for(int i=1; i<=k; i++) if(mark2[i]) {
int x = a[i].x, y = a[i].y, p = mark2[i]; mark2[i] = 0;
if(g[x].count(y+1)) v = g[x][y+1], link(i, v), mark[v] = 1;
if(g[x].count(y-1)) v = g[x][y-1], link(i, v), mark[v] = 1;
if(p == 1 && g[x-1].count(y)) v = g[x-1][y], link(i, v), mark[v] = 1;
if(p == 2 && g[x+1].count(y)) v = g[x+1][y], link(i, v), mark[v] = 1;
}
for(int i=1; i<=k; i++) if(mark[i]) ins(i+k, t, INF);
}
namespace mf {
int q[N], head, tail, vis[N], d[N];
bool bfs() {
memset(vis, 0, sizeof(vis));
head = tail = 1;
q[tail++] = s; d[s] = 0; vis[s] = 1;
while(head != tail) {
int u = q[head++];
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v] && e[i].c > e[i].f) {
int v = e[i].v;
vis[v] = 1; d[v] = d[u]+1;
q[tail++] = v;
if(v == t) return true;
}
}
return false;
}
int cur[N];
int dfs(int u, int a) {
if(u == t || a == 0) return a;
int flow = 0, f;
for(int &i=cur[u];i;i=e[i].ne)
if(d[e[i].v] == d[u]+1 && (f = dfs(e[i].v, min(a, e[i].c - e[i].f)) ) >0 ) {
flow += f;
e[i].f += f;
e[i^1].f -= f;
a -= f;
if(a == 0) break;
}
if(a) d[u] = -1;
return flow;
}
int dinic() {
int flow = 0;
while(bfs()) {
for(int i=s; i<=t; i++) cur[i] = h[i];
flow += dfs(s, INF);
}
return flow;
}
}
int main() {
freopen("in", "r", stdin);
m=read(); n=read(); k=read();
for(int i=1; i<=k; i++) a[i].x = read(), a[i].y = read(), a[i].w = read(), g[a[i].x][a[i].y] = i;
build();
int ans = mf::dinic();
printf("%d\n", ans);
}
bzoj 4823: [Cqoi2017]老C的方块 [最小割]的更多相关文章
- bzoj4823: [Cqoi2017]老C的方块(最小割)
4823: [Cqoi2017]老C的方块 题目:传送门 题解: 毒瘤题ORZ.... 太菜了看出来是最小割啥边都不会建...狂%大佬强强强 黑白染色?不!是四个色一起染,四层图跑最小割... 很 ...
- BZOJ 4823: [Cqoi2017]老C的方块
分析: 我觉得我的网络流白学了...QAQ... 其实数据范围本是无法用网络流跑过去的,然而出题者想让他跑过去,也就跑过去了... 看到题目其实感觉很麻烦,不知道从哪里入手,那么仔细观察所给出的有用信 ...
- bzoj 4823 [Cqoi2017]老C的方块——网络流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4823 一个不合法方案其实就是蓝线的两边格子一定选.剩下两部分四相邻格子里各选一个. 所以这个 ...
- BZOJ 4823 [Cqoi2017]老C的方块 ——网络流
lrd的题解:http://www.cnblogs.com/liu-runda/p/6695139.html 我还是太菜了.以后遇到这种题目应该分析分析性质的. 网络流复杂度真是$O(玄学)$ #in ...
- bzoj 4823: [Cqoi2017]老C的方块【最大权闭合子图】
参考:https://www.cnblogs.com/neighthorn/p/6705785.html 并不是黑白染色而是三色染色(还有四色的,不过是一个意思 仔细观察一下不合法情况,可以发现都是特 ...
- bzoj 4823 & 洛谷 P3756 老C的方块 —— 最小割
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4823 https://www.luogu.org/problemnew/show/P3756 ...
- 【BZOJ4823】[CQOI2017]老C的方块(网络流)
[BZOJ4823][CQOI2017]老C的方块(网络流) 题面 BZOJ 题解 首先还是给棋盘进行黑白染色,然后对于特殊边左右两侧的格子单独拎出来考虑. 为了和其他格子区分,我们把两侧的这两个格子 ...
- BZOJ 4823 Luogu P3756 [CQOI2017]老C的方块 (网络流、最小割)
题目链接 (Luogu) https://www.luogu.org/problem/P3756 (BZOJ) http://lydsy.com/JudgeOnline/problem.php?id= ...
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...
随机推荐
- hdu_1711Number Sequence(kmp)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 0/1背包 dp学习~6
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 I NEED A OFFER! Time Limit: 2000/1000 MS (Java/O ...
- 大区间素数筛选(POJ 2689)
/* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...
- Replacement(思维题)
题目链接:http://codeforces.com/problemset/problem/570/C C. Replacement time limit per test 2 seconds mem ...
- JAVA实现网页上传头像
大概实现就是在页面嵌入一个file类型的input控件,并且将之隐藏,点击上传传递到这个控件上面,选择文件,将图片以base64的方式传递到后台,后台解码器解码,保存图片,并且把图片名字保存到数据库或 ...
- c语言基础学习06
=============================================================================涉及到的知识点有:1.C语言库函数.字符输入函 ...
- 制作ssh互信的docker镜像
Dockerfile FROM ubuntu:16.04 # package RUN apt-get update; apt-get -y install ssh COPY ssh_config /e ...
- 小白的Python之路 day5 模块XML特点和用法
模块XML的特点和用法 一.简介 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今 ...
- GitHub上传文件或项目的教程
既然是往GitHub上传文件,那GitHub账号必须得有,这时候就会有同学问:妖怪吧,我没有GitHub账号怎么办? 别急别急,打开GitHub网站https://github.com/,然后注册就O ...
- vue项目中使用ueditor
以vue-cli生成的项目为例 1.static文件夹下先放入ueditor文件 2.index.html添加如下代码 <script type="text/javascript& ...