[BZOJ3504] [Cqoi2014]危桥(网络流)

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3504


还是比较套路的网络流。
首先是st->a1连an流量,然后a2->ed连an流量
相同的st->b1连bn流量,然后b2->ed连bn流量
其他的边,如果是普通边就i->j连INF流量,如果是危桥就i->j连1流量(为什么是1流量?因为一个来回的概念是去一次然后沿原路返回,危桥能过2次吗,所以这里要设置流量为2/2=1)
重点来了,显而易见的我们这样连边,会有a1流到b2去的情况,那应该怎么办呢?很简单,只要把b1和b2交换在跑一边,如果两次都能流到终点an+bn这么多的流量,那就合法,否则不合法。
为什么呢?假设a1流到的是b2,那么b2会流到b1,这样b1流到a2的时候就相当于是a1->a2了 。


code:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int INF=999999999;
struct node
{
    int x,y,c,next,other;
}a[2110000]; int len,last[1110000];
int st,ed;
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 list[1110000],head,tail,h[1110000];
bool bt()
{
    memset(h,0,sizeof(h)); h[st]=1;
    list[1]=st;
    head=1,tail=2;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(a[k].c>0 && h[y]==0)
            {
                h[y]=h[x]+1;
                list[tail++]=y;
            }
        }
        head++;
    }
    if(h[ed]>0) return true;
    else return false;
}
int mymin(int x,int y) {return x<y?x:y;}
int findflow(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(a[k].c>0 && h[y]==h[x]+1 && s<f)
        {
            t=findflow(y,mymin(f-s,a[k].c));
            s+=t; a[k].c-=t; a[a[k].other].c+=t;
        }
    }
    if(s==0) h[x]=0;
    return s;
}
char ss[51];
int map[51][51];
int n,a1,b1,a2,b2,an,bn;
bool build()
{
    st=0,ed=n+1;
    len=0; memset(last,0,sizeof(last));
    ins(st,a1,an); ins(st,b1,bn);
    ins(a2,ed,an); ins(b2,ed,bn);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            if(map[i][j]==1) ins(i,j,INF);
            else if(map[i][j]==2) ins(i,j,1);
        }
    int s=0,t;
    while(bt()==true)
    {
        s+=findflow(st,INF) ;
    }
    if(s==an+bn) return true;
    else return false;
}
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",ss+1);
            for(int j=1;j<=n;j++)
            {
                if(ss[j]=='N') map[i][j]=1;
                else if(ss[j]=='O') map[i][j]=2;
                else map[i][j]=0;
            }
        }
        bool t1=false;
        if(build())
        {
            swap(b1,b2);
            t1=build();
        }
        if(t1){printf("Yes\n");continue;}
        else{printf("No\n");continue;}
    }
    return 0;
}   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值