此题是构造好题。
首先我们对体中的方块进行四染色:
from sdfzyhx的博客
然后我们发现每一个不好的四个方块都是Y->B->R->G
然后分四层跑网络流dinic(记得中间2、3层不要赋值为inf,我挂在这好久)
#include
#define gc getchar()
#define N 100009
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define ll long long
#define inf 100000000
using namespace std;
int C,R,n,x[N],y[N],w[N];
map,int> p;
struct edge
{
int from,to,cap,flow;
edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector e;
vector G[N];
int d[N],cur[N];
bool vis[N];
int read()
{
char ch;
int x=1;
while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
int s=ch-'0';
while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0';
return s*x;
}
void add(int from,int to,int cap)
{
e.push_back(edge(from,to,cap,0));
e.push_back(edge(to,from,0,0));
int m=e.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
#define E e[G[x][i]]
bool bfs(int s,int t)
{
memset(vis,0,sizeof(vis));
queue Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while (!Q.empty())
{
int x=Q.front();
Q.pop();
for (int i=0;iE.flow)
{
vis[E.to]=1;
d[E.to]=d[x]+1;
Q.push(E.to);
}
}
return vis[t];
}
int dfs(int x,int a,int s,int t)
{
if (x==t||a==0) return a;
int flow=0,f;
for (int &i=cur[x];i0)
{
E.flow+=f;
e[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if (!a) break;
}
return flow;
}
int Maxflow(int s,int t)
{
int flow=0;
while (bfs(s,t))
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,inf,s,t);
}
return flow;
}
#undef E
int main()
{
C=read(),R=read(),n=read();
for (int i=1;i<=n;i++) x[i]=read(),y[i]=read(),w[i]=read(),p[mp(x[i],y[i])]=i;
for (int i=1;i<=n;i++)
switch (x[i]%4)
{
case 1:
if (y[i]&1)
{
if (p.count(mp(x[i],y[i]-1)))
add(p[mp(x[i],y[i]-1)],i,inf);
if (p.count(mp(x[i],y[i]+1)))
add(p[mp(x[i],y[i]+1)],i,inf);
if (p.count(mp(x[i]-1,y[i])))
add(p[mp(x[i]-1,y[i])],i,inf);
if (p.count(mp(x[i]+1,y[i])))
add(i,p[mp(x[i]+1,y[i])],min(w[i],w[p[mp(x[i]+1,y[i])]]));
}
else add(n+2,i,w[i]);
break;
case 2:
if (y[i]&1)
{
if (p.count(mp(x[i],y[i]-1)))
add(i,p[mp(x[i],y[i]-1)],inf);
if (p.count(mp(x[i],y[i]+1)))
add(i,p[mp(x[i],y[i]+1)],inf);
if (p.count(mp(x[i]+1,y[i])))
add(i,p[mp(x[i]+1,y[i])],inf);
}
else add(i,n+1,w[i]);
break;
case 3:
if (y[i]&1) add(i,n+1,w[i]);
else
{
if (p.count(mp(x[i],y[i]-1)))
add(i,p[mp(x[i],y[i]-1)],inf);
if (p.count(mp(x[i],y[i]+1)))
add(i,p[mp(x[i],y[i]+1)],inf);
if (p.count(mp(x[i]-1,y[i])))
add(i,p[mp(x[i]-1,y[i])],inf);
}
break;
case 0:
if (y[i]&1) add(n+2,i,w[i]);
else
{
if (p.count(mp(x[i],y[i]-1)))
add(p[mp(x[i],y[i]-1)],i,inf);
if (p.count(mp(x[i],y[i]+1)))
add(p[mp(x[i],y[i]+1)],i,inf);
if (p.count(mp(x[i]+1,y[i])))
add(p[mp(x[i]+1,y[i])],i,inf);
if (p.count(mp(x[i]-1,y[i])))
add(i,p[mp(x[i]-1,y[i])],min(w[i],w[p[mp(x[i]-1,y[i])]]));
}
break;
}
printf("%d\n",Maxflow(n+2,n+1));
return 0;
}