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\n",ans);
    return 0;
}
posted @ 2020-11-29 15:27  Seaway-Fu  阅读(139)  评论(0编辑  收藏  举报