zhber
有好多做过的题没写下来,如果我还能记得就补吧

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
 
刷刷水题有益身体健康……
网络流sb题
就是从1出发或者到达n的边的权为1,其他为inf
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
#define S 1
#define T n
using namespace std;
inline LL read()
{
    LL x=0,f=1;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;
}
int n,cnt=1,ans;
struct edge{
    int to,next,v;
}e[1000010];
int head[200010];
int h[200010];
int q[200010];
inline void ins(int u,int v,int w)
{
    e[++cnt].to=v;
    e[cnt].v=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}
inline void insert(int u,int v,int w)
{
    ins(u,v,w);
    ins(v,u,0);
}
inline bool bfs()
{
    memset(h,-1,sizeof(h));
    int t=0,w=1;
    h[S]=0;q[1]=S;
    while (t<w)
    {
        int now=q[++t];
        for (int i=head[now];i;i=e[i].next)
          if (e[i].v&&h[e[i].to]==-1)
            {
                h[e[i].to]=h[now]+1;
                q[++w]=e[i].to;
            }
    }
    if (h[T]==-1)return 0;
    return 1;
}
inline int dfs(int x,int f)
{
    if (x==T||!f)return f;
    int w,used=0;
    for (int i=head[x];i;i=e[i].next)
      if (e[i].v&&h[e[i].to]==h[x]+1)
      {
        w=dfs(e[i].to,min(e[i].v,f-used));
        e[i].v-=w;
        e[i^1].v+=w;
        used+=w;
        if (used==f)return f;
      }
    if (!used)h[x]=-1;
    return used;
}
inline void dinic()
{while(bfs())ans+=dfs(S,inf);}
int main()
{
    n=read();
    for (int i=1;i<n;i++)
    {
        int x=read();
        for (int j=1;j<=x;j++)
        {
            int to=read();
            if (i==1||to==n)insert(i,to,1);
            else insert(i,to,inf);
        }
    }
    dinic();
    printf("%d\n",ans);
}

  

posted on 2014-12-09 18:57  zhber  阅读(150)  评论(0编辑  收藏  举报