标签:zjoi2009 https span clu gre ret stat pop 定义
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 7 #define get(i,j) ((i-1)*m+j) 8 const int INF=0x7f7f7f7f; 9 const int MAXN=400000; 10 const int xx[4]={-1,1,0,0},yy[4]={0,0,-1,1}; 11 12 struct Edge 13 { 14 int to,w,next; 15 }E[MAXN]; 16 int node=1,head[MAXN],dis[MAXN],mp[200][200]; 17 int s=0,t=10001; 18 int n,m,ans; 19 20 void insert(int u,int v,int w) 21 { 22 E[++node]=(Edge){v,w,head[u]}; 23 head[u]=node; 24 E[++node]=(Edge){u,0,head[v]}; 25 head[v]=node; 26 } 27 28 bool bfs() 29 { 30 memset(dis,-1,sizeof(dis)); 31 queue<int> Q; 32 Q.push(s); 33 dis[s]=0; 34 while(!Q.empty()) 35 { 36 int q=Q.front();Q.pop(); 37 for(int i=head[q];i;i=E[i].next) 38 if(E[i].w&&dis[E[i].to]==-1) 39 { 40 Q.push(E[i].to); 41 dis[E[i].to]=dis[q]+1; 42 } 43 } 44 return dis[t]!=-1; 45 } 46 47 int dfs(int x,int flow) 48 { 49 if(x==t) return flow; 50 int w,used=0; 51 for(int i=head[x];i;i=E[i].next) 52 if(E[i].w&&dis[E[i].to]==dis[x]+1) 53 { 54 w=flow-used; 55 w=dfs(E[i].to,min(w,E[i].w)); 56 E[i].w-=w; 57 E[i^1].w+=w; 58 used+=w; 59 if(used==flow)return flow; 60 } 61 if(!used) dis[x]=-1; 62 return used; 63 } 64 65 void dinic() 66 { 67 while(bfs()) ans+=dfs(s,INF); 68 } 69 70 int main() 71 { 72 scanf("%d%d",&n,&m); 73 for(int i=1;i<=n;i++) 74 for(int j=1;j<=m;j++) 75 { 76 scanf("%d",&mp[i][j]); 77 if(mp[i][j]==1) insert(s,get(i,j),INF); 78 else if(mp[i][j]==2) insert(get(i,j),t,INF); 79 } 80 for(int i=1;i<=n;i++) 81 for(int j=1;j<=m;j++) 82 for(int k=0;k<4;k++) 83 { 84 int x=i+xx[k],y=j+yy[k]; 85 if(x<1||x>n||y<1||y>m) continue; 86 if(mp[i][j]!=1||mp[x][y]!=1) 87 insert(get(i,j),get(x,y),1); 88 } 89 dinic(); 90 printf("%d",ans); 91 return 0; 92 }
标签:zjoi2009 https span clu gre ret stat pop 定义
原文地址:https://www.cnblogs.com/InWILL/p/9735856.html