zoukankan      html  css  js  c++  java
  • BZOJ2929: [Poi1999]洞穴攀行

    【传送门:BZOJ2929


    简要题意:

      给出n个洞穴,起点为1,终点为n,并给出一个有向无环图,且保证每条路径的终点都是n

      起点和终点所连接的边只能走一次,其他的边能走无限次,求最多能从起点派多少人到达终点


    题解:

      裸网络流,直接将走的次数转化为流量就ok了


    参考代码:

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    struct node
    {
        int x,y,c,next,other;
    }a[110000];int len,last[210];
    int n,m,st,ed;
    void ins(int x,int y,int c)
    {
        int k1=++len,k2=++len;
        a[k1].x=x;a[k1].y=y;a[k1].c=c;
        a[k1].next=last[x];last[x]=k1;
        a[k2].x=y;a[k2].y=x;a[k2].c=0;
        a[k2].next=last[y];last[y]=k2;
        a[k1].other=k2;
        a[k2].other=k1;
    }
    int list[210],h[210];
    bool bt_h()
    {
        memset(h,0,sizeof(h));h[st]=1;
        list[1]=st;
        int 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(h[y]==0&&a[k].c>0)
                {
                    h[y]=h[x]+1;
                    list[tail++]=y;
                }
            }
            head++;
        }
        if(h[ed]>0) return true;
        return false;
    }
    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((h[y]==h[x]+1)&&a[k].c>0&&f>s)
            {
                t=findflow(y,min(a[k].c,f-s));
                s+=t;
                a[k].c-=t;a[a[k].other].c+=t;
            }
        }
        if(s==0) h[x]=0;
        return s;
    }
    int main()
    {
        scanf("%d",&n);
        len=0;memset(last,0,sizeof(last));
        st=1;ed=n;
        for(int i=1;i<n;i++)
        {
            int k;
            scanf("%d",&k);
            for(int j=1;j<=k;j++)
            {
                int y;
                scanf("%d",&y);
                if(i==1||y==n) ins(i,y,1);
                else ins(i,y,999999999);
            }
        }
        int ans=0;
        while(bt_h()==true)
        {
            ans+=findflow(st,999999999);
        }
        printf("%d
    ",ans);
        return 0;
    }

     

  • 相关阅读:
    SQLServer限制IP,限制用户,限制SSMS登录
    关于Natively Compiled Stored Procedures的优化
    .NET/C#- EPPLUS DEMO
    ASP.NET MVC- 视图
    ASP.NET MVC- 在Area里使用RedirectToAction跳转出错的解决方法
    ASP.NET MVC- 解决HTML转码
    ASP.NET MVC- KindEditor的使用
    ASP.NET MVC- Upload File的例子
    ASP.NET MVC- ActionFilter的使用
    ASP.NET MVC- UrlHelper的用法
  • 原文地址:https://www.cnblogs.com/Never-mind/p/8621415.html
Copyright © 2011-2022 走看看