BZOJ2929 [Poi1999]洞穴攀行

187 篇文章 0 订阅
58 篇文章 0 订阅

标签:网络流,最大流

题目

题目传送门

Description

洞穴学者在Byte Mountain的Grate Cave里组织了一次训练。训练中,每一位洞穴学者要从最高的一个室到达最底下的一个室。他们只能向下走。一条路上每一个连续的室都要比它的前一个低。此外,每一个洞穴学者都要从最高的室出发,沿不同的路走到最低的室。
限制:
1.起点连接的通道同一时间只能容纳一个人通过
2.终点连接的通道同一时间只能容纳一个人通过
3.其他边都很宽敞,同一时间可以容纳无限多的人
问:可以有多少个人同时参加训练?
Input

第一行有一个整数n(2<=n<=200),等于洞穴中室的个数。用1~n给室标号,号码越大就在越下面。最高的室记为1,最低的室记为n。以下的n-1行是对通道的描述。第I+1行包含了与第I个室有通道的室(只有比标号比I大的室)。这一行中的第一个数是m,0<=m<=(n-i+1),表示被描述的通道的个数。接着的m个数字是与第I个室有通道的室的编号。

Output

输出一个整数。它等于可以同时参加训练的洞穴学者的最大人数。

Sample Input

12

4 3 4 2 5

1 8

2 9 7

2 6 11

1 8

2 9 10

2 10 11

1 12

2 10 12

1 12

1 12
Sample Output

3

分析

直接套用最大流模板

code

#include<bits/stdc++.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,b) for(register int i=a;i<=b;i++)
#define dep(i,a,b) for(register int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define inf 0x7fffffff
using namespace std;
inline ll read()
{
    ll f=1,x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int maxn=206;
struct edge{int to,next,v;}e[maxn*maxn];
int h[maxn],que[maxn],last[maxn],cnt=1,ans=0,n;
void insert(int u,int v,int w){
    e[++cnt]=(edge){v,last[u],w};last[u]=cnt;
    e[++cnt]=(edge){u,last[v],0};last[v]=cnt;
}

bool bfs()
{
    int head=0,tail=1,now;
    mem(h,-1);
    que[0]=1;h[1]=0;
    while(head<tail){
        now=que[head++];
#define reg(x) for(int i=last[x];i;i=e[i].next)
        reg(now)
            if(e[i].v&&h[e[i].to]==-1){
                h[e[i].to]=h[now]+1;
                que[tail++]=e[i].to;
            }
    }
    if(h[n]==-1)return 0;else return 1;
}

int dfs(int x,int f)
{
    if(x==n)return f;
    int w,used=0;
    reg(x)
        if(e[i].v&&h[e[i].to]==h[x]+1){
            w=f-used;w=dfs(e[i].to,min(e[i].v,w));
            e[i].v-=w;e[i^1].v+=w;
            used+=w;if(used==f)return f;
        }
    if(!used)h[x]=-1;
    return used;
}
void dinic(){while(bfs())ans+=dfs(1,inf);}
int main()
{
    n=read();
    rep(i,1,n-1){
        int m=read();
        while(m--){
            int x=read();
            if(i==1||x==n)insert(i,x,1);
            else insert(i,x,inf);
        }
    }
    dinic();
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值