BZOJ 3504 危桥(网络流)

题目链接:BZOJ 3504

题目大意:n(n<=50)座岛,互相之间有或者没有双向边相连,有的边可以走两次,有的可以走无限次,问是否可以从a1~a2往返an次、从b1~b2往返bn次。

题解:最大流,建图比较显然,用流量限制边走的次数。因为是双向边,往返就是走两次。所以,S向a1、b1连边,a2、b2向T连边,边权为2an、2bn,判断S是否满流。只是需要注意一下,还要重新建图,S改向a1、b2连边,T改向a2,b1连边,再求一次最大流,避免a1走到b2之类的情况。两遍都满流才合法。

code(新学了当前弧优化(⊙^⊙))

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define inf 1000000000
using namespace std;
inline int read()
{
    char c=getchar(); int num=0,f=1;
    while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
    while (c<='9'&&c>='0') { num=num*10+c-'0'; c=getchar(); }
    return num*f;
}
struct edge{
    int to,ne,v;
}e[100005];
int n,a1,a2,b1,b2,an,bn;
int tot=1,head[205],last[205],S,T,ans,height[205];
char s[55][55];
void push(int x,int y,int v)
{
    e[++tot].to=y; e[tot].v=v; e[tot].ne=head[x]; head[x]=tot;
    e[++tot].to=x; e[tot].v=v; e[tot].ne=head[y]; head[y]=tot;
}
bool bfs()
{
    memset(height,-1,sizeof(height));
    queue<int> q; q.push(S); height[S]=1;
    while (!q.empty())
    {
        int now=q.front(); q.pop();
        for (int i=head[now];i;i=e[i].ne)
        {
            int v=e[i].to;
            if (e[i].v&&height[v]==-1)
            {
                height[v]=height[now]+1;
                q.push(v);
            }
        }
    }
    return height[T]!=-1;
}
int dfs(int now,int flow)
{
    if (now==T) return flow;
    int used=0;
    for (int& i=last[now];i;i=e[i].ne)
    {
        int v=e[i].to;
        if (e[i].v&&height[v]==height[now]+1)
        {
            int w=dfs(v,min(flow-used,e[i].v));
            e[i].v-=w; e[i^1].v+=w; used+=w;
            if (used==flow) return used;
        }
    }
    if (!used) height[now]=-1;
    return used;
}
int main()
{ 
    while (~scanf("%d",&n))
    {
        S=0; T=n+1;
        a1=read()+1; a2=read()+1; an=read();
        b1=read()+1; b2=read()+1; bn=read();
        tot=1; memset(head,0,sizeof(head));
        push(S,a1,an*2),push(a2,T,an*2);
        push(S,b1,bn*2),push(b2,T,bn*2);
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s[i]);
            for (int j=i;j<n;j++)
            {
                if (s[i][j]=='O') push(i,j+1,2);
                 else if (s[i][j]=='N') push(i,j+1,inf);
            }
        }
        ans=0;
        while (bfs())
        {
            for (int i=0;i<=T;i++) last[i]=head[i];
            ans+=dfs(S,inf); 
        }
        if (ans!=an*2+bn*2) { printf("No\n"); continue; } 
        tot=1; memset(head,0,sizeof(head));
        push(S,a1,an*2),push(a2,T,an*2);
        push(S,b2,bn*2),push(b1,T,bn*2);
        for (int i=1;i<=n;i++)
        {
            for (int j=i;j<n;j++)
            {
                if (s[i][j]=='O') push(i,j+1,2);
                 else if (s[i][j]=='N') push(i,j+1,inf);
            }
        }
        ans=0; 
        while (bfs())
        {
            for (int i=0;i<=T;i++) last[i]=head[i];
            ans+=dfs(S,inf); 
        }
        if (ans!=an*2+bn*2) { printf("No\n"); continue; }
        printf("Yes\n");    
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值