一种网络流建图的思路吧,改天最好整理一波网络流建图思路
1 #include <bits/stdc++.h> 2 using namespace std; 3 int n,h,t,a1,a2,an,b1,b2,bn,flow,now;char ch; 4 int dis[52],l[52],d[52][52];char c[52][52]; 5 char getch() 6 { 7 for(ch=getchar();ch!='O' && ch!='N' && ch!='X';ch=getchar()); 8 return ch; 9 } 10 bool bfs() 11 { 12 for(int i=1;i<=n+1;i++) 13 dis[i]=0; 14 for(h=1,t=1,l[1]=0,dis[0]=1;h<=t;h++) 15 for(int i=1;i<=n+1;i++) 16 if(d[l[h]][i] && !dis[i]) 17 l[++t]=i,dis[i]=dis[l[h]]+1; 18 return dis[n+1]; 19 } 20 int dfs(int now,int maxflow) 21 { 22 if(now==n+1) return maxflow; 23 int flow=0; 24 for(int i=0;i<=n+1;i++) 25 if(dis[i]==dis[now]+1 && d[now][i]) 26 { 27 int thi=dfs(i,min(maxflow,d[now][i])); 28 d[now][i]-=thi;d[i][now]+=thi; 29 maxflow-=thi;flow+=thi; 30 if(maxflow==0) return flow; 31 } 32 if(flow==0) 33 dis[now]=-1; 34 return flow; 35 } 36 void INIT() 37 { 38 for(int i=1;i<=n;i++) 39 for(int j=1;j<=n;j++) 40 switch(c[i][j]) 41 { 42 case'O':d[j][i]=d[i][j]=2;break; 43 case'N':d[j][i]=d[i][j]=flow;break; 44 case'X':d[j][i]=d[i][j]=0;break; 45 } 46 for(int i=0;i<=n+1;i++) 47 d[0][i]=d[i][0]=d[i][n+1]=d[n+1][i]=0; 48 d[a1][0]=d[0][a1]=an<<1;d[b1][0]=d[0][b1]=bn<<1; 49 d[a2][n+1]=d[n+1][a2]=an<<1;d[b2][n+1]=d[n+1][b2]=bn<<1; 50 now=0; 51 } 52 int main() 53 { 54 while(~scanf("%d%d%d%d%d%d%d",&n,&a1,&a2,&an,&b1,&b2,&bn)) 55 { 56 a1++;a2++;b1++;b2++; 57 flow=an+bn<<1; 58 for(int i=1;i<=n;i++) 59 for(int j=1;j<=n;j++) 60 c[i][j]=getch(); 61 INIT(); 62 while(now<flow && bfs()) now=now+dfs(0,flow); 63 if(now<flow) 64 { 65 puts("No"); 66 continue; 67 } 68 swap(b1,b2); 69 INIT(); 70 while(now<flow && bfs()) now=now+dfs(0,flow); 71 if(now<flow) 72 puts("No"); 73 else 74 puts("Yes"); 75 } 76 return 0; 77 }