题解:最大流
s连as bs
at bt连t
求最大流是否==af+bf
但是这样不对
因为as可能流到bt
所以再
s连as bt
at bs连t
再求一遍最大流
相当于让as流到bt的流流不过去
少打了一个+号WA了好几发
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; const int maxn=109; const int oo=10000000; int n; int as,at,af; int bs,bt,bf; int ma[maxn][maxn]; struct Edge{ int from,to,cap,flow; }; vector<int>G[maxn]; vector<Edge>edges; void Addedge(int x,int y,int z){ Edge e; e.from=x;e.to=y;e.cap=z;e.flow=0; edges.push_back(e); e.from=y;e.to=x;e.cap=z;e.flow=0; edges.push_back(e); int c=edges.size(); G[x].push_back(c-2); G[y].push_back(c-1); } int s,t; queue<int>q; int vis[maxn]; int cur[maxn]; int d[maxn]; int Bfs(){ memset(vis,0,sizeof(vis)); vis[s]=1;d[s]=0;q.push(s); while(!q.empty()){ int x=q.front();q.pop(); for(int i=0;i<G[x].size();++i){ Edge e=edges[G[x][i]]; if((e.cap>e.flow)&&(!vis[e.to])){ vis[e.to]=1;d[e.to]=d[x]+1; q.push(e.to); } } } return vis[t]; } int Dfs(int x,int a){ if((x==t)||(a==0))return a; int nowflow=0,f=0; for(int i=cur[x];i<G[x].size();++i){ cur[x]=i; Edge e=edges[G[x][i]]; if((d[x]+1==d[e.to])&&((f=Dfs(e.to,min(a,e.cap-e.flow)))>0)){ nowflow+=f;a-=f; edges[G[x][i]].flow+=f; edges[G[x][i]^1].flow-=f; if(a==0)break; } } return nowflow; } int Maxflow(){ int flow=0; while(Bfs()){ memset(cur,0,sizeof(cur)); flow+=Dfs(s,oo); } return flow; } void Dinicinit(){ while(!q.empty())q.pop(); edges.clear(); for(int i=0;i<maxn;++i)G[i].clear(); } char ss[maxn]; int main(){ while(scanf("%d%d%d%d%d%d%d",&n,&as,&at,&af,&bs,&bt,&bf)!=EOF){ ++as;++at;++bs;++bt; for(int i=1;i<=n;++i){ scanf("%s",ss+1); for(int j=1;j<=n;++j){ if(ss[j]==‘X‘)ma[i][j]=0; if(ss[j]==‘O‘)ma[i][j]=1; if(ss[j]==‘N‘)ma[i][j]=oo; } } s=n+1;t=n+2; int fla=1; Dinicinit(); for(int i=1;i<=n;++i){ for(int j=i+1;j<=n;++j){ if(ma[i][j])Addedge(i,j,ma[i][j]); } } Addedge(s,as,af); Addedge(s,bs,bf); Addedge(at,t,af); Addedge(bt,t,bf); if(Maxflow()!=af+bf)fla=0; Dinicinit(); for(int i=1;i<=n;++i){ for(int j=i+1;j<=n;++j){ if(ma[i][j])Addedge(i,j,ma[i][j]); } } Addedge(s,as,af); Addedge(s,bt,bf); Addedge(at,t,af); Addedge(bs,t,bf); if(Maxflow()!=af+bf)fla=0; if(fla)printf("Yes\n"); else printf("No\n"); } return 0; }