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);
}