zoukankan      html  css  js  c++  java
  • 【bzoj2625】[Neerc2009]Inspection 有上下界最小流

    题目描述

    You are in charge of a team that inspects a new ski resort. A ski resort is situated on several mountains and consists of a number of slopes. Slopes are connected with each other, forking and joining. A map of the ski resort is represented as an acyclic directed graph. Nodes of the graph represent different points in ski resort and edges of the graph represent slopes between the points, with the direction of edges going downwards. 
    Your team has to inspect each slope of the ski resort. Ski lifts on this resort are not open yet, but you have a helicopter. In one fiight the helicopter can drop one person into any point of the resort. From the drop off point the person can ski down the slopes, inspecting each slope as they ski. It is fine to inspect the same slope multiple times, but you have to minimize the usage of the helicopter. So, you have to figure out how to inspect all the slopes with the fewest number of helicopter flights.
    给张有向无环图,问至少多少条路径能够覆盖所有的边(可以重复

    输入

    The first line of the input file contains a single integer number n (2 <= n <= 100) - the number of points in the ski resort. The following n lines of the input file describe each point of the ski resort numbered from 1 to n. Each line starts with a single integer number mi (0 <= mi < n for i from 1 to n) and is followed by mi integer numbers aij separated by spaces. All aij are distinct for each i and each aij (1 <= aij <= n, aij 
e i) represents a slope going downwards from point i to point aij . Each point in the resort has at least one slope connected to it.

    输出

    On the first line of the output file write a single integer number k - the minimal number of helicopter flights that are needed to inspect all slopes. Then write k lines that describe inspection routes for each helicopter flight. Each route shall start with single integer number from 1 to n - the number of the drop off point for the helicopter flight, followed by the numbers of points that will be visited during inspection in the corresponding order as the slopes are inspected going downwards. Numbers on a line shall be separated by spaces. You can write routes in any order.

    样例输入

    8
    1 3
    1 7
    2 4 5
    1 8
    1 8
    0
    2 6 5
    0

    样例输出

    4


    题解

    有上下界最小流

    题目要求可以从任意点出发到任意点停止,每条边至少经过一次。这显然是最小流问题。

    连边S->每个点、每个点->T、T->S、原图中的边,容量为inf;对于每个点x如果入度大于0则连SS->x,容量为ind[x],否则连x->TT,容量为-ind[x]。

    跑SS->TT最大流,然后把T->S、SS->x、x->TT边删除,跑T->S最大流,两次答案相减即为答案。

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #define N 110
    #define M 20010
    using namespace std;
    const int inf = 1 << 30;
    queue<int> q;
    int head[N] , to[M] , val[M] , next[M] , cnt = 1 , s , t , dis[N] , ind[N];
    void add(int x , int y , int z)
    {
        to[++cnt] = y , val[cnt] = z , next[cnt] = head[x] , head[x] = cnt;
        to[++cnt] = x , val[cnt] = 0 , next[cnt] = head[y] , head[y] = cnt;
    }
    bool bfs()
    {
        int x , i;
        memset(dis , 0 , sizeof(dis));
        while(!q.empty()) q.pop();
        dis[s] = 1 , q.push(s);
        while(!q.empty())
        {
            x = q.front() , q.pop();
            for(i = head[x] ; i ; i = next[i])
            {
                if(val[i] && !dis[to[i]])
                {
                    dis[to[i]] = dis[x] + 1;
                    if(to[i] == t) return 1;
                    q.push(to[i]);
                }
            }
        }
        return 0;
    }
    int dinic(int x , int low)
    {
        if(x == t) return low;
        int temp = low , i , k;
        for(i = head[x] ; i ; i = next[i])
        {
            if(val[i] && dis[to[i]] == dis[x] + 1)
            {
                k = dinic(to[i] , min(temp , val[i]));
                if(!k) dis[to[i]] = 0;
                val[i] -= k , val[i ^ 1] += k;
                if(!(temp -= k)) break;
            }
        }
        return low - temp;
    }
    int main()
    {
        int n , i , b , e , x , y , ans = 0;
        scanf("%d" , &n) , b = 0 , e = n + 1 , s = n + 2 , t = n + 3;
        add(e , b , inf);
        for(i = 1 ; i <= n ; i ++ )
        {
            scanf("%d" , &y);
            while(y -- ) scanf("%d" , &x) , add(i , x , inf) , ind[i] -- , ind[x] ++ ;
        }
        for(i = 1 ; i <= n ; i ++ )
        {
            add(b , i , inf) , add(i , e , inf);
            if(ind[i] > 0) add(s , i , ind[i]);
            else add(i , t , -ind[i]);
        }
        while(bfs()) dinic(s , inf);
        ans = val[3] , val[2] = val[3] = 0;
        for(i = head[s] ; i ; i = next[i]) val[i] = val[i ^ 1] = 0;
        for(i = head[t] ; i ; i = next[i]) val[i] = val[i ^ 1] = 0;
        s = e , t = b;
        while(bfs()) ans -= dinic(s , inf);
        printf("%d
    " , ans);
        return 0;
    }
    

     

  • 相关阅读:
    JMeter学习-016-思路篇之-山重水复柳暗花明
    JMeter学习-015-JMeter 断言之-Bean Shell Assertion
    JMeter学习-014-JMeter 配置元件实例之
    Fiddler-008-简单模拟性能测试
    Fiddler-007-修改HTTP请求响应数据
    Fiddler-006-修改HTTP请求参数
    JMeter学习-013-JMeter 逻辑控制器之-如果(If)控制器
    JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
    JMeter学习-011-JMeter 后置处理器实例之
    JMeter学习-010-JMeter 配置元件实例之
  • 原文地址:https://www.cnblogs.com/GXZlegend/p/7301374.html
Copyright © 2011-2022 走看看