zoukankan      html  css  js  c++  java
  • 求割边最小的最小割

    hdu3987

    Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest. Harry need stop them as soon as.

    The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.
     

    Input
    Input consists of several test cases.

    The first line is number of test case.

    Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1.

    Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1).

    Technical Specification

    1. 2 <= n <= 1000
    2. 0 <= m <= 100000
    3. 0 <= u, v <= n-1
    4. 0 < c <= 1000000
    5. 0 <= d <= 1
     

    Output
    For each test case:
    Output the case number and the answer of how many roads are blocked at least.
     

    Sample Input
    3 4 5 0 1 3 0 0 2 1 0 1 2 1 1 1 3 1 1 2 3 3 1 6 7 0 1 1 0 0 2 1 0 0 3 1 0 1 4 1 0 2 4 1 0 3 5 1 0 4 5 2 0 3 6 0 1 1 0 0 1 2 0 1 1 1 1 1 2 1 0 1 2 1 0 2 1 1 1
     

    Sample Output
    Case 1: 3 Case 2: 2 Case 3: 2
    题意:给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路,使得从点0无法到达点n-1。破坏每条路都有一个代价。求在代价最小的前提下,最少需要破坏多少条道路。(就是说求在最小割的前提下,最小的割边数)

    第一种:
        建边的时候每条边权 w = w * (E + 1) + 1;
        这样得到最大流 maxflow / (E + 1) ,最少割边数 maxflow % (E + 1)

      道理很简单,如果原先两类割边都是最小割,那么求出的最大流相等
        但边权变换后只有边数小的才是最小割了

        乘(E+1)是为了保证边数叠加后依然是余数,不至于影响求最小割的结果

      第二种:

        建图,得到最大流后,图中边若满流,说明该边是最小割上的边

      再建图,原则:满流的边改为容量为 1 的边,未满流的边改为容量 INF 的边,然后最大流即答案

    程序:

    #include"stdio.h"
    #include"string.h"
    #define M 100009
    #define inf 999999999
    int min(int a,int b)
    {
        return a<b?a:b;
    }
    struct st
    {
        int u,v,w,next;
    }edge[500009];
    int head[M],work[M],q[M],dis[M],t;
    void init()
    {
        t=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v,int w)
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].w=w;
        edge[t].next=head[u];
        head[u]=t++;
    
        edge[t].u=v;
        edge[t].v=u;
        edge[t].w=0;
        edge[t].next=head[v];
        head[v]=t++;
    }
    int bfs(int S,int T)
    {
        int rear=0;
        memset(dis,-1,sizeof(dis));
        q[rear++]=S;
        dis[S]=0;
        for(int i=0;i<rear;i++)
        {
            for(int j=head[q[i]];j!=-1;j=edge[j].next)
            {
                int v=edge[j].v;
                if(edge[j].w&&dis[v]==-1)
                {
                    dis[v]=dis[q[i]]+1;
                    q[rear++]=v;
                    if(v==T)
                        return 1;
                }
            }
        }
        return 0;
    }
    int dfs(int cur,int a,int T)
    {
        if(cur==T)
            return a;
        for(int &i=work[cur];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(edge[i].w&&dis[v]==dis[cur]+1)
            {
                int tt=dfs(v,min(a,edge[i].w),T);
                if(tt)
                {
                    edge[i].w-=tt;
                    edge[i^1].w+=tt;
                    return tt;
                }
            }
        }
        return 0;
    }
    int Dinic(int S,int T)
    {
        int ans=0;
        while(bfs(S,T))
        {
            memcpy(work,head,sizeof(head));
            while(int tt=dfs(S,inf,T))
                ans+=tt;
        }
        return ans;
    }
    int main()
    {
        int T,i;
        int kk=1;
        scanf("%d",&T);
        while(T--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            init();
            while(m--)
            {
                int a,b,c,d;
                scanf("%d%d%d%d",&a,&b,&c,&d);
                add(a,b,c);
                if(d==1)
                    add(b,a,c);
            }
            Dinic(0,n-1);
            for(i=0;i<t;i+=2)
            {
                if(edge[i].w==0)
                {
                    edge[i].w=1;
                    edge[i^1].w=0;
                }
    
                else
                {
                    edge[i].w=inf;
                    edge[i^1].w=0;
                }
    
            }
            int sum=Dinic(0,n-1);
            printf("Case %d: %d
    ",kk++,sum);
        }
    }


  • 相关阅读:
    shell快捷键
    通过调整tcp参数来防范DDOS攻击
    解决 nf_conntrack: table full, dropping packet 的几种思路
    Linux系统资源限制
    解决Out of socket memory问题
    wrk简介
    部分 TCP 参数简介
    P1706 全排列问题
    P1149 [NOIP2008 提高组] 火柴棒等式
    P1104 生日
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348237.html
Copyright © 2011-2022 走看看