bzoj3504: [Cqoi2014]危桥
传送门:http://http://www.lydsy.com/JudgeOnline/problem.php?id=3504
思路:证明见此
http://blog.csdn.net/wzq_QwQ/article/details/46546977
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> const int maxn=110,maxm=10010,inf=1e9; using namespace std; int n,a1,a2,b1,b2,an,bn,map[maxn][maxn],S=maxn-2,T=maxn-1;char ch[maxn];bool cant; int pre[maxm],now[maxn],son[maxm],val[maxm],tot=1,head,tail,q[maxn+10],dis[maxn]; void add(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;} void ins(int a,int b,int c){add(a,b,c),add(b,a,0);} void build(){ memset(now,0,sizeof(now)),tot=1; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (map[i][j]==1) ins(i,j,2); else if (map[i][j]==2) ins(i,j,inf); } bool bfs(){ memset(dis,-1,sizeof(dis)); q[tail=1]=S,dis[S]=head=0; while (head!=tail){ if (++head>maxn) head=1; int x=q[head]; for (int y=now[x];y;y=pre[y]) if (dis[son[y]]==-1&&val[y]){ if (++tail>maxn) tail=1; dis[son[y]]=dis[x]+1,q[tail]=son[y]; } } return dis[T]>0; } int find(int x,int low){ if (x==T) return low; int y,res=0; for (y=now[x];y;y=pre[y]){ if (dis[son[y]]!=dis[x]+1||!val[y]) continue; int tmp=find(son[y],min(low,val[y])); res+=tmp,low-=tmp,val[y]-=tmp,val[y^1]+=tmp; if (!low) break; } if (!y) dis[x]=-1; return res; } int main(){ while (scanf("%d%d%d%d%d%d%d",&n,&a1,&a2,&an,&b1,&b2,&bn)!=EOF){ a1++,b1++,a2++,b2++,cant=0; memset(map,0,sizeof(map)); for (int i=1;i<=n;i++){ scanf("%s",ch+1); for (int j=1;j<=n;j++){ if (ch[j]=='O') map[i][j]=1; else if (ch[j]=='N') map[i][j]=2; } } build(),ins(S,a1,an*2),ins(a2,T,an*2),ins(S,b1,bn*2),ins(b2,T,bn*2); int ans=0; while (bfs()) ans+=find(S,inf); if (ans<(an+bn)*2) cant=1; if (!cant){ build(); ins(S,a1,an*2),ins(a2,T,an*2),ins(S,b2,bn*2),ins(b1,T,bn*2); ans=0;while (bfs()) ans+=find(S,inf); if (ans<(an+bn)*2) cant=1; } puts(cant?"No":"Yes"); } return 0; }