啊一道好题
感觉写得挺爽的啊这题
这种有一点懵逼然后学了一点东西之后很明朗的感觉真是好!
预处理参考 :http://www.cppblog.com/MatoNo1/archive/2014/11/01/142798.html?opt=admin
建图参考 :http://hihocoder.com/problemset/problem/1398?sid=1010444
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<vector> #define N 80000 #define mod #define inf 300000000 #define rg register using namespace std; typedef long long ll; int n,m,tot=0,cnt=0,tmp=0,s,t; int dis[N],hd[N],head[N],c[N],in[N],d[(N<<3)],w[N]; bool del[N]; struct hh{ int to,nxt,w; }b1[N*100],b[N*100]; int p (int x,int y) { return (x-1)*m+y; } void link(int x,int y,int w){ b1[tot].nxt=hd[x]; b1[tot].to=y; b1[tot].w=w; hd[x]=tot++; } void lnk(int x,int y,int w){ b[tot].nxt=head[x]; b[tot].to=y; b[tot].w=w; head[x]=tot++; } void dele(int x){ del[x]=1; for (int i=hd[x];i!=-1;i=b1[i].nxt) if (!del[b1[i].to]) dele(b1[i].to); } void top(){ int h=0,t=0; for (int i=1;i<=cnt;++i){ if (!in[i]) d[++t]=i; else del[i]=1; } while (h<=t){ int x=d[++h]; for (int i=hd[x];i!=-1;i=b1[i].nxt){ in[b1[i].to]--,del[b1[i].to]=0; if (!in[b1[i].to]) d[++t]=b1[i].to; } } for (int i=1;i<=cnt;++i) if (del[i]) dele(i); } bool bfs(){ queue <int> q; memset(dis,-1,sizeof(dis)); q.push(s);dis[s]=0; while (!q.empty()){ int h=q.front(); q.pop(); for (int i=head[h];i!=-1;i=b[i].nxt){ int v=b[i].to; if (dis[v]!=-1||b[i].w<=0) continue; dis[v]=dis[h]+1; if (v==t) return 1; q.push(v); } } return dis[t]==-1?0:1; } int dfs(int u,int f){ if (u==t||!f) return f; int res=f; for (int &i=c[u];i!=-1;i=b[i].nxt){ int v=b[i].to; if (dis[v]!=dis[u]+1||b[i].w<=0) continue; int d=dfs(v,min(res,b[i].w)); b[i].w-=d,b[i^1].w+=d; res-=d; if (res<=0) break; } return f-res; } int dinic(){ int ans=0; while (bfs()){ for (int i=s;i<=t;++i) c[i]=head[i]; ans+=dfs(s,inf); } return ans; } int main(){ freopen ("pvz.in","r",stdin); freopen ("pvz.out","w",stdout); memset(hd,-1,sizeof(hd)); memset(head,-1,sizeof(head)); scanf ("%d%d",&n,&m); int x,y,z; for (int i=1;i<=n;++i) for (int j=1;j<=m;++j){ scanf ("%d%d",&w[p(i,j)],&z); for (int k=1;k<=z;++k) scanf ("%d%d",&x,&y),link(p(i,j),p(x+1,y+1),0),in[p(x+1,y+1)]++; } for (int i=1;i<=n;++i) for (int j=2;j<=m;++j) link(p(i,j),p(i,j-1),0),in[p(i,j-1)]++; cnt=n*m; top(); s=0,t=cnt+1,tmp=0; tot=0; for (int i=1;i<=cnt;++i){ if (del[i]) continue; if (w[i]>0) tmp+=w[i],lnk(i,t,w[i]),lnk(t,i,0); else lnk(s,i,-w[i]),lnk(i,s,0); for (int j=hd[i];j!=-1;j=b1[j].nxt) if (!del[b1[j].to]) lnk(i,b1[j].to,inf),lnk(b1[j].to,i,0); } tmp-=dinic(); printf ("%d\n",max(0,tmp)); return 0; }
时间: 2024-10-14 06:27:25