洛谷——P2936 [USACO09JAN]全流Total Flow

题目描述

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.

The system will provide extra test case feedback for your first 50 submissions.

约翰总希望他的奶牛有足够的水喝,因此他找来了农场的水管地图,想算算牛棚得到的水的 总流量.农场里一共有N根水管.约翰发现水管网络混乱不堪,他试图对其进行简 化.他简化的方式是这样的:

两根水管串联,则可以用较小流量的那根水管代替总流量.

两根水管并联,则可以用流量为两根水管流量和的一根水管代替它们

当然,如果存在一根水管一端什么也没有连接,可以将它移除.

请写个程序算出从水井A到牛棚Z的总流量.数据保证所有输入的水管网络都可以用上述方法 简化.

输入输出格式

输入格式:

  • 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

输出格式:

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

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

3 

网络流模板题,注意字母存在大小写!

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1001000
#define inf 9999999
using namespace std;
char ch;
queue<int>q;
int s,e,n,m,x,y,z,tot=1,ans;
int to[N],cap[N],cnt[N],lev[N],head[N],nextt[N];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x*f;
}
int add(int x,int y,int z)
{
    tot++;to[tot]=y;cap[tot]=z;nextt[tot]=head[x];head[x]=tot;
    tot++;to[tot]=x;cap[tot]=0;nextt[tot]=head[y];head[y]=tot;
}
int bfs()
{
    while(!q.empty()) q.pop();
    for(int i=s;i<=n;i++)
    {
        lev[i]=-1;
        cnt[i]=head[i];
    }
    q.push(s),lev[s]=0;
    while(!q.empty())
    {
        x=q.front();q.pop();
        for(int i=head[x];i;i=nextt[i])
        {
            int t=to[i];
            if(lev[t]==-1&&cap[i]>0)
            {
                lev[t]=lev[x]+1;
                q.push(t);
                if(t==e) return true;
            }
        }
    }
    return false;
}
int dinic(int x,int flow)
{
    if(x==e) return flow;
    int delta,rest=0;
    for(int &i=cnt[x];i;i=nextt[i])
    {
        int t=to[i];
        if(cap[i]>0&&lev[t]>lev[x])
        {
            delta=dinic(t,min(cap[i],flow-rest));
            if(delta)
            {
                cap[i]-=delta;
                cap[i^1]+=delta;
                rest+=delta;
                if(rest==flow) break;
            }
        }
    }
    if(rest!=flow) lev[x]=-1;
    return rest;
}
int main()
{
    n=read();
    s=1;e=26;
    for(int i=1;i<=n;i++)
    {
        cin>>ch;x=ch-‘A‘+1;
        cin>>ch;y=ch-‘A‘+1;
        z=read();
        add(x,y,z);
    }
    n=‘z‘-‘A‘+1;
    while(bfs())
        ans+=dinic(s,inf);
    printf("%d",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/z360/p/8321744.html

时间: 2024-10-10 06:02:20

洛谷——P2936 [USACO09JAN]全流Total Flow的相关文章

洛谷 P2936 [USACO09JAN]全流Total Flow

题目描述 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 appa

[USACO09JAN]全流Total Flow

题目描述 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 appa

洛谷P3128 [USACO15DEC]最大流Max Flow

洛谷P3128 [USACO15DEC]最大流Max Flow 题目描述 FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N.所有隔间都被管道连通了. FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti.一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少. 输入输出格式 输入格式: The first line of the input contains

洛谷 P3128 [USACO15DEC]最大流Max Flow

P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1…N. Each pipe connects a pair of stalls,

洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk

洛谷——P3128 [USACO15DEC]最大流Max Flow

https://www.luogu.org/problem/show?pid=3128 题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-

洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk

洛谷——P2935 [USACO09JAN]最好的地方Best Spot

P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 <= F <= P) favorite pastures F_i of the P (1 <= P <= 500; 1 <= F_i <= P) total pastures (conveniently nu

洛谷 P2935 [USACO09JAN]最好的地方Best Spot

P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 <= F <= P) favorite pastures F_i of the P (1 <= P <= 500; 1 <= F_i <= P) total pastures (conveniently nu