bzoj3504: [Cqoi2014] dangerous bridge (maximum flow)

Keywords: network

Title gate
.

Solution:
As soon as I see this question.
Shout out "don't you finish building the map and run the network flow?"
Mr. Zhu came and patted me: if a1 flows to b2, the reverse will do..
conversely?? What the hell?
emmmm well, then I gave up the treatment to decadent.

I see the question again..
What the hell..
conversely??
Oh, the other way around..
The reverse seems to work..
After the normal completion of the drawing.
Turn back b1b2.
If the original stream. Then the reverse will certainly not cross flow.

Code implementation:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node {int x,y,c,next,other;}a[110000];int len,last[110];
void ins(int x,int y,int c) {
    int k1,k2;
    len++;k1=len;a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
    len++;k2=len;a[len].x=y;a[len].y=x;a[len].c=0;a[len].next=last[y];last[y]=len;
    a[k1].other=k2;a[k2].other=k1;
}
int head,tail,list[110],st,ed,h[110];
bool bt_h() {
    head=1;tail=2;list[1]=st;memset(h,0,sizeof(h));h[st]=1;
    while(head!=tail) {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next) {
            int y=a[k].y;
            if(h[y]==0&&a[k].c>0) {h[y]=h[x]+1;list[tail++]=y;}
        }head++;
    }if(h[ed]==0)return false;return true;
}
int find_flow(int x,int f) {
    if(x==ed)return f;
    int s=0,t;
    for(int k=last[x];k;k=a[k].next) {
        int y=a[k].y;
        if(h[y]==h[x]+1&&a[k].c>0&&s<f) {
            t=find_flow(y,min(a[k].c,f-s));s+=t;a[k].c-=t;a[a[k].other].c+=t;
        }
    }if(s==0)h[x]=0;return s;

}
char map[110][110];int n,a1,a2,an,b1,b2,bn;
void build() {
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) {
        if(map[i][j]=='O')ins(i,j,2);else if(map[i][j]=='N')ins(i,j,999999999);
    }
}
int main() {
    while(scanf("%d",&n)!=EOF) {
        scanf("%d%d%d%d%d%d",&a1,&a2,&an,&b1,&b2,&bn);
        a1++;a2++;b1++;b2++;
        for(int i=1;i<=n;i++)scanf("%s",map[i]+1);
        st=n+1;ed=st+1;build();
        ins(st,a1,2*an);ins(a2,ed,2*an);
        ins(st,b1,2*bn);ins(b2,ed,2*bn);
        int ans=0;while(bt_h()==true)ans+=find_flow(st,999999999);
        if(ans<2*(an+bn))printf("No\n");
        else {
            build();
            ins(st,a1,2*an);ins(a2,ed,2*an);
            ins(st,b2,2*bn);ins(b1,ed,2*bn);
            int ans=0;while(bt_h()==true)ans+=find_flow(st,999999999);
            if(ans<2*(an+bn))printf("No\n");
            else printf("Yes\n");
        }
    }
    return 0;
}

Posted by Axem on Tue, 31 Mar 2020 05:09:29 -0700