题目描述

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;
,ans;
int to[N],cap[N],cnt[N],lev[N],head[N],nextt[N];
int read()
{
    ,f=; char ch=getchar();
    ;ch=getchar();}
    +ch-',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]=;nextt[tot]=head[y];head[y]=tot;
}
int bfs()
{
    while(!q.empty()) q.pop();
    for(int i=s;i<=n;i++)
    {
        lev[i]=-;
        cnt[i]=head[i];
    }
    q.push(s),lev[s]=;
    while(!q.empty())
    {
        x=q.front();q.pop();
        for(int i=head[x];i;i=nextt[i])
        {
            int t=to[i];
            &&cap[i]>)
            {
                lev[t]=lev[x]+;
                q.push(t);
                if(t==e) return true;
            }
        }
    }
    return false;
}
int dinic(int x,int flow)
{
    if(x==e) return flow;
    ;
    for(int &i=cnt[x];i;i=nextt[i])
    {
        int t=to[i];
        &&lev[t]>lev[x])
        {
            delta=dinic(t,min(cap[i],flow-rest));
            if(delta)
            {
                cap[i]-=delta;
                cap[i^]+=delta;
                rest+=delta;
                if(rest==flow) break;
            }
        }
    }
    ;
    return rest;
}
int main()
{
    n=read();
    s=;e=;
    ;i<=n;i++)
    {
        cin>>ch;x=ch-;
        cin>>ch;y=ch-;
        z=read();
        add(x,y,z);
    }
    n=;
    while(bfs())
        ans+=dinic(s,inf);
    printf("%d",ans);
    ;
}

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

  1. 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)

    P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...

  2. 洛谷 P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  3. 【luogu P2936 [USACO09JAN]全流Total Flow】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2936 菜 #include <queue> #include <cstdio> #i ...

  4. AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  5. [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  6. P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]

    题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...

  7. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

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

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

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

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

随机推荐

  1. 编程语言BrainkFuck

    BrainFuck由Urban Müller在1993年创建,是经常被吐槽的语言,不过我觉得除了名字其它都还挺正常的,没错我觉得这个语言设计的很正常没有Fuck到我的脑子,大概是因为我根本就没有脑子吧 ...

  2. GRUB (简体中文)

    原文链接:https://wiki.archlinux.org/index.php/GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 前言 引导程序是计算机启动时 ...

  3. [转]大整数算法[11] Karatsuba乘法

    ★ 引子         前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) ...

  4. 64位linux安装32位校园网客户端

    下面的是ubuntu下和arch下的安装方法,ubuntu的转自网络, ubuntu: 下载客户端并解压 安装开发包 1 sudo -i 2 dpkg --add-architecture i386 ...

  5. C#之WinForm基础 新建一个不可编辑的comboBox

    慈心积善融学习,技术愿为有情学.善心速造多好事,前人栽树后乘凉.我今于此写经验,愿见文者得启发. 1.拉控件 2.添加可选数据 3.改变基本样式 4.效果图 C#优秀,值得学习.Winform,WPF ...

  6. URL的井号

    2010年9月,twitter改版. 一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为 http://twitter.com/username 改版后,就变 ...

  7. java基础79 会话管理(Cookie技术、Session技术)

    1.概念     会话管理:管理浏览器和服务器之间会话过程中产生的会话数据.    Cookie技术:会话数据保存到浏览器客户端.[存 编号/标记(id)]    Session技术:会话技术会保存到 ...

  8. Oracle JDeveloper 10g 卡顿、花屏的解决方法

    1.JDeveloper 10g花屏的解决办法: 在Win7或WinXP环境下,JDeveloper10g可能产生花屏现象,给开发者造成困扰,解决方法如下: 打开{JDEV_HOME}\jdev\bi ...

  9. php-fpm和cgi,并发响应的理解以及高并发和多线程的关系

    首先搞清楚php-fpm与cgi的关系 cgi cgi是一个web server与cgi程序(这里可以理解为是php解释器)之间进行数据传输的协议,保证了传递的是标准数据. php-cgi php-c ...

  10. 读取文件和输入——read 脚本命令

    Linux之read命令使用 read命令: read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 1)read后面的变量var可以只有一个,也可以有多个,这时如果输入 ...