Reprint Please specify: http://blog.csdn.net/jiangshibiao/article/details/22844219
"Original question"
3504: [Cqoi2014] Dangerous bridge Time Limit:10 Sec Memory limit:128 MB
submit:89 solved:65
[Submit] [Status] Description
Alice and Bob live in a country of N-Islands, numbered 0 to N-1. Some islands are connected by bridges, and the roads on the bridge are double
But only one person can pass at a time. Some of these bridges are only two times as long as they become dangerous bridges in disrepair. Alice wants to travel between the islands Al and A2 (from Al to A2 and from A2 to Al for a round trip). At the same time, Bob wants to travel between the island BL and B2 to bn times. In this process, all dangerous bridges can pass up to two times, and the rest of the bridges will be accessible indefinitely. Could Alice and Bob finish their wishes?
Input
There are several sets of test data.
The first row of each group of data contains 7 spaces separated by integers, N, AL, A2, an, BL, B2, BN, respectively.
Next is a symmetric matrix of n rows n columns, consisting of uppercase letters. The I-line j column of the Matrix describes the connection between the islands of number I 11 and j-l, and if "O" means a dangerous Bridge: "N" means that there is an ordinary bridge connected: "X" means that no bridge is connected.
| Output
For each set of test data output line, if they can complete the wish output "Yes", otherwise output "No".
Sample Input 4 0 1 1 2 3 1
Xoxx
Oxox
XOXO
Xxox
4 0 2 1 1 3 2
Xnxo
Nxox
XOXO
Oxox
Sample Output Yes
No
Data range
4<=n<50
O<=A1, A2, B1, B2<=n-1
1 <=an. b<=50
"Analysis" because it is two people go together, we can find not one to do the network flow, but should do together. Set the super source point 0 and the Super sink point N, if the maximum flow is, it means correct. ----->>>>> But there's a problem with this: for example, if you can't get to A2 from A1, it eventually flows to B2, which is obviously not feasible. Then we can put B1 and B2 back to do it again, if it is full of flow can be.
Code
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int n=101;
const int inf=2100000000;
int q[n],f[n],map[n][n],temp[n][n];
int n,s1,e1,s2,e2,q1,q2,i,j,flow,tt;
Char C,enter;
BOOL Flag;
BOOL BFs () {memset (q,0,sizeof (q));
memset (F,-1,sizeof (f));
int h=0,t=1;f[0]=1;
while (h<t) {int now=q[++h];if (now==n) return 1;
for (int i=0;i<=n;i++) if (map[now][i]&&f[i]==-1) {q[++t]=i;
f[i]=f[now]+1;
}} return 0;
} int dinic (int sta,int sum) {if (sta==n) return sum;
int os=sum;
for (int i=0;i<=n;i++) if (map[sta][i]&&f[i]==f[sta]+1) {int min=dinic (i,min (Os,map[sta][i]));
Map[sta][i]-=min;map[i][sta]+=min;os-=min;
} if (os==sum) f[sta]=-1;
return Sum-os; } int main () {while (scanf ("%d%d%d%d%d%d%d", &N,&S1,&E1,&Q1,&S2,&E2,&Q2)!=eof) {s1++;
e1++;s2++;e2++;
memset (map,0,sizeof (map)); memset (tEmp,0,sizeof (temp));
for (i=1;i<=n;i++) {scanf ("%c", &enter);
for (j=1;j<=n;j++) {scanf ("%c", &c);
if (c== ' O ') map[i][j]=temp[i][j]=2;else if (c== ' N ') Map[i][j]=temp[i][j]=inf;
}} flag=true;
flow=0;map[0][s1]=q1*2;map[0][s2]=q2*2;map[e1][++n]=q1*2;map[e2][n]=q2*2;
while (BFS ()) {tt=dinic (0,inf);
Flow+=tt;
} if (flow<2* (Q1+Q2)) Flag=false;
if (flag) {memset (map,0,sizeof (map));
for (i=1;i<n;i++) for (j=1;j<n;j++) map[i][j]=temp[i][j];
map[0][s1]=q1*2;map[0][e2]=q2*2;map[e1][n]=q1*2;map[s2][n]=q2*2;flow=0;
while (BFS ()) {tt=dinic (0,inf);
Flow+=tt;
} if (flow<2* (Q1+Q2)) Flag=false;
} if (flag) printf ("yes\n"); else printf ("no\n");
} return 0; }