BZOJ2929洞穴攀行

2929: [Poi1999]洞穴攀行
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 172 Solved: 86
Description
一队洞穴学者在Byte Mountain的Grate Cave里组织了一次训练。训练中,每一位洞穴学者要从最高的一个室到达最底下的一个室。他们只能向下走。一条路上每一个连续的室都要比它的前一个低。此外,每一个洞穴学者都要从最高的室出发,沿不同的路走到最低的室。问:可以有多少个人同时参加训练?
任务:
写一个程序:
l 读入对洞穴的描述。
l 计算可以同时参加训练的人数。
l 将结果输出。
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
只有连接1和n的边只能走一次。。
然后就是裸最大流了。。
请原谅我不是1A。。
附上本蒟蒻的代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,k,x,cnt=1,h[40001],q[40001],head,tail,dis[40001],ans,sum;
struct kx
{
    int to,next,v;
};
kx edge[40001];

int read()
{
    int w=0,c=1;
    char ch=getchar();
    while (ch<'0' || ch>'9')
      {
        if (ch=='-')
          c=-1;
        ch=getchar();
      }
    while (ch>='0' && ch<='9')
      {
        w=w*10+ch-'0';
        ch=getchar();
      }
    return w*c;
}

void add(int u,int v,int w)
{
    cnt++,edge[cnt].next=h[u],h[u]=cnt,edge[cnt].to=v,edge[cnt].v=w;
}

void insert(int u,int v,int w)
{
    add(u,v,w),add(v,u,0);
}

bool bfs()
{
    int j,p;
    memset(dis,-1,sizeof(dis));
    q[1]=1,dis[1]=1;
    head=0,tail=1;
    while (head<tail)
      {
        head++;
        j=q[head];
        p=h[j];
        while (p)
          {
            if (dis[edge[p].to]<0 && edge[p].v>0)
              {
                dis[edge[p].to]=dis[j]+1;
                tail++;
                q[tail]=edge[p].to;
              }
            p=edge[p].next;
          }
      }
    if (dis[n]>0)
      return true;
    else
      return false;
}

int dfs(int x,int f)
{
    int w,used=0,i=h[x];
    if (x==n)
      return f;
    while (i)
      {
        if (edge[i].v && dis[edge[i].to]==dis[x]+1)
          {
            w=f-used;
            w=dfs(edge[i].to,min(w,edge[i].v));
            edge[i].v-=w;
            edge[i^1].v+=w;
            used+=w;
            if (used==f)
              return f;
          }
        i=edge[i].next;
      }
    if (!used)
      dis[x]=-1;
    return used;
}

int main()
{
    int i,j;
    n=read();
    for (i=1;i<=n-1;i++)
      {
        x=read();
        for (j=1;j<=x;j++)
          {
            k=read();
            if (i==1 || k==n)
              insert(i,k,1);
            else
              insert(i,k,0x7fffffff);
          }
      }
    ans=0;
    while (bfs())
      while (sum=dfs(1,0x7fffffff))
        ans+=sum;
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值