标签:
空地之间开始没有连然后一直WA。。。题意混乱。。。尴尬。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,n) for(int i=1;i<=n;i++) #define clr(x,c) memset(x,c,sizeof(x)) int read(){ int x=0;char c=getchar();bool f=true; while(!isdigit(c)) { if(c==‘-‘) f=false; c=getchar(); } while(isdigit(c)) x=x*10+c-‘0‘,c=getchar(); return f?x:-x; } int xx[5]={0,0,0,1,-1}; int yy[5]={0,1,-1,0,0}; const int nmax=10005; const int maxn=100005; const int inf=0x7f7f7f7f; struct edge{ int to,cap;edge *next,*rev; }; edge edges[maxn],*pt,*head[nmax],*p[nmax],*cur[nmax]; int cnt[nmax],h[nmax],g[105][105]; void add(int u,int v,int d){ pt->to=v;pt->cap=d;pt->next=head[u];head[u]=pt++; } void adde(int u,int v,int d){ add(u,v,d);add(v,u,0);head[u]->rev=head[v];head[v]->rev=head[u]; } int maxflow(int s,int t,int n){ clr(cnt,0);cnt[0]=n;clr(h,0); int flow=0,a=inf,x=s;edge *e; while(h[s]<n){ for(e=cur[x];e;e=e->next) if(e->cap>0&&h[x]==h[e->to]+1) break; if(e){ a=min(a,e->cap);p[e->to]=cur[x]=e;x=e->to; if(x==t){ while(x!=s) p[x]->cap-=a,p[x]->rev->cap+=a,x=p[x]->rev->to; flow+=a,a=inf; } }else{ if(!--cnt[h[x]]) break; h[x]=n; for(e=head[x];e;e=e->next) if(e->cap>0&&h[x]>h[e->to]+1) h[x]=h[e->to]+1,cur[x]=e; cnt[h[x]]++; if(x!=s) x=p[x]->rev->to; } } return flow; } int main(){ pt=edges;clr(head,0); int n=read(),m=read(),s=0,t=n*m+1; rep(i,n) rep(j,m) g[i][j]=read(); rep(i,n) rep(j,m){ if(g[i][j]==2) adde(m*(i-1)+j,t,inf); else if(g[i][j]==1) adde(s,m*(i-1)+j,inf); rep(k,4){ int tx=i+xx[k],ty=j+yy[k]; if(tx&&ty&&tx<=n&&ty<=m&&(g[i][j]!=g[tx][ty]||!(g[i][j]|g[tx][ty]))) adde(m*(i-1)+j,m*(tx-1)+ty,1); } } printf("%d\n",maxflow(s,t,t+1)); return 0; }
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5659908.html