zoukankan      html  css  js  c++  java
  • USACO Total Flow

    USACO Total Flow

    洛谷传送门

    JDOJ传送门

    Description

    Farmer John always wants his cows to have enough water and thus has
    made a map of the N (1 <= N <= 700) water pipes on the farm that
    connect the well to the barn. He was surprised to find a wild mess
    of different size pipes connected in an apparently haphazard way.
    He wants to calculate the flow through the pipes.

    Two pipes connected in a row allow water flow that is the minimum
    of the values of the two pipe's flow values. The example of a pipe
    with flow capacity 5 connecting to a pipe of flow capacity 3 can
    be reduced logically to a single pipe of flow capacity 3:

      +---5---+---3---+    ->    +---3---+
    

    Similarly, pipes in parallel let through water that is the sum of
    their flow capacities:

        +---5---+
     ---+       +---    ->    +---8---+
        +---3---+
    

    Finally, a pipe that connects to nothing else can be removed; it
    contributes no flow to the final overall capacity:

        +---5---+
     ---+               ->    +---3---+
        +---3---+--
    

    All the pipes in the many mazes of plumbing can be reduced using
    these ideas into a single total flow capacity.

    Given a map of the pipes, determine the flow capacity between the
    well (A) and the barn (Z).

    Consider this example where node names are labeled with letters:

                     +-----------6-----------+
            A+---3---+B                      +Z
                     +---3---+---5---+---4---+
                             C       D
    

    Pipe BC and CD can be combined:

                     +-----------6-----------+
            A+---3---+B                      +Z
                     +-----3-----+-----4-----+
                                 D
    

    Then BD and DZ can be combined:

                     +-----------6-----------+
            A+---3---+B                      +Z
                     +-----------3-----------+
    

    Then two legs of BZ can be combined:

                     B
            A+---3---+---9---+Z
    

    Then AB and BZ can be combined to yield a net capacity of 3:

            A+---3---+Z
    

    Write a program to read in a set of pipes described as two endpoints
    and then calculate the net flow capacity from 'A' to 'Z'. All
    networks in the test data can be reduced using the rules here.

    Pipe i connects two different nodes a_i and b_i (a_i in range
    'A-Za-z'; b_i in range 'A-Za-z') and has flow F_i (1 <= F_i <=
    1,000). Note that lower- and upper-case node names are intended
    to be treated as different.

    Input

    * Line 1: A single integer: N

    * Lines 2..N + 1: Line i+1 describes pipe i with two letters and an
    integer, all space-separated: a_i, b_i, and F_i

    Output

    * Line 1: A single integer that the maximum flow from the well ('A')
    to the barn ('Z')

    Sample Input

    5 A B 3 B C 3 C D 5 D Z 4 B Z 6

    Sample Output

    3


    题解:

    网络最大流裸题,源点为1,汇点为26.

    剩下的就是个DInic的板子。

    代码:

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #define int long long
    using namespace std;
    const int maxn=210;
    const int maxm=5010;
    const int INF=2147483646;
    int n,m,s,t,ans;
    int tot=1,head[maxn],nxt[maxm<<1],to[maxm<<1],val[maxm<<1];
    int lv[maxn],cur[maxn];
    queue<int> q;
    void add(int x,int y,int z)
    {
        to[++tot]=y;
        nxt[tot]=head[x];
        val[tot]=z;
        head[x]=tot;
    }
    bool bfs()
    {
        memset(lv,0,sizeof(lv));
        lv[s]=1;
        q.push(s);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=head[x];i;i=nxt[i])
            {
                int y=to[i];
                if(!val[i]||lv[y])
                    continue;
                lv[y]=lv[x]+1;
                q.push(y);
            }
        }
        return lv[t];
    }
    int dfs(int x,int flow)
    {
        if(!flow||x==t)
            return flow;
        int ret=0;
        for(int i=cur[x];i;i=nxt[i])
        {
            cur[x]=i;
            int y=to[i];
            if(val[i]>0 && lv[y]==lv[x]+1)
            {
                int tmp=dfs(y,min(val[i],flow));
                flow-=tmp;
                ret+=tmp;
                val[i]-=tmp;
                val[i^1]+=tmp;
            }
        }
        return ret;
    }
    signed main()
    {
        scanf("%lld",&m);
        s=1,t=26;
        for(int i=1;i<=m;i++)
        {
            char s1[4],s2[4];
            int x,y,z;
            scanf("%s%s%lld",s1,s2,&z);
            x=s1[0]-'A'+1;
            y=s2[0]-'A'+1;
            add(x,y,z);
            add(y,x,0);
        }
        while(bfs())
        {
            memcpy(cur,head,sizeof(head));
            ans+=dfs(s,INF);
        }
        printf("%lld
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    用python抓取百度指数 以及 用cxfreeze打包的经验
    selenium中send_keys的使用
    python之文件调用
    学习python之selenium
    学习python图像识别
    解决方案: 运行ugarchroll,报错Error in try(.C("c_qstd", p = as.double(p), mu = as.double(mu), sigma = as.double(sigma), : NA/NaN/Inf in foreign function call (arg 3)
    int 转 const char*
    均值,方差,协方差,协方差矩阵,特征值,特征向量
    OpenCv 图片上添加汉字
    OpenCV获取与设置像素点的值的几个方法
  • 原文地址:https://www.cnblogs.com/fusiwei/p/14056427.html
Copyright © 2011-2022 走看看