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

一眼题,最大流模板,这道题主要考我们输入的处理,其它就只和你写的dinic" role="presentation" style="position: relative;">dinicdinic有关了。

代码如下:

#include<bits/stdc++.h>
#define N 1000
#define M 100005
using namespace std;
int cnt=-1,m,s,t,first[N],d[N];
struct Node{int v,next,c;}e[M];
inline void add(int u,int v,int c){
    e[++cnt].v=v,e[cnt].c=c,e[cnt].next=first[u],first[u]=cnt;
    e[++cnt].v=u,e[cnt].c=0,e[cnt].next=first[v],first[v]=cnt;
}
inline bool bfs(){
    queue<int>q;
    memset(d,-1,sizeof(d));
    q.push(s),d[s]=0;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=first[x];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(d[v]!=-1||e[i].c<=0)continue;
            d[v]=d[x]+1;
            if(v==t)return true;
            q.push(v);
        }
    }
    return false;
}
inline int dfs(int x,int f){
    if(x==t||!f)return f;
    int flow=f;
    for(int i=first[x];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(e[i].c>0&&d[v]==d[x]+1&&flow){
            int tmp=dfs(v,min(flow,e[i].c));
            if(!tmp)d[v]=-1;
            e[i].c-=tmp;
            e[i^1].c+=tmp;
            flow-=tmp;
        }
    }
    return f-flow;
}
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans;
}
int main(){
    cnt=-1;
    memset(first,-1,sizeof(first));
    m=read(),s='A'-'A'+1,t='Z'-'A'+1;
    for(int i=1;i<=m;++i){
        char c[2];
        int u,v,w;
        scanf("%s",c);
        u=(int)(c[0]-'A'+1);
        scanf("%s%d",c,&w);
        v=(int)(c[0]-'A'+1);
        add(u,v,w);
    }
    int ans=0;
    while(bfs())ans+=dfs(s,0x3f3f3f3f);
    printf("%d",ans);
    return 0;
}

2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)的更多相关文章

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

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

  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. 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)

    P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...

  4. 2018.07.17 洛谷P1368 工艺(最小表示法)

    传送门 好的一道最小表示法的裸板,感觉跑起来贼快(写博客时评测速度洛谷第二),这里简单讲讲最小表示法的实现. 首先我们将数组复制一遍接到原数组队尾,然后维护左右指针分别表示两个即将进行比较的字符串的头 ...

  5. 2018.11.06 洛谷P1099 树网的核(最短路+枚举)

    传送门 之前看李煜东的书一直感觉是道神题. 然后发现这题数据范围只有300?300?300? 直接上floydfloydfloyd然后暴力就完了啊. 代码: #include<bits/stdc ...

  6. 2018.11.06 洛谷P1941 飞扬的小鸟(背包)

    传送门 上升看成完全背包. 下降看成01背包. 注意边界转移就行了. 代码: #include<bits/stdc++.h> using namespace std; inline int ...

  7. 2018.07.01 洛谷小B的询问(莫队)

    P2709 小B的询问 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...

  8. 2018.07.31洛谷P1552 [APIO2012]派遣(可并堆)

    传送门 貌似是个可并堆的模板题,笔者懒得写左偏堆了,直接随机堆水过.实际上这题就是维护一个可合并的大根堆一直从叶子合并到根,如果堆中所有数的和超过了上限就一直弹直到所有数的和不超过上限为止,最后对于当 ...

  9. 2018.07.23 洛谷P4097 [HEOI2013]Segment(李超线段树)

    传送门 给出一个二维平面,给出若干根线段,求出x" role="presentation" style="position: relative;"&g ...

随机推荐

  1. java ee7 软件安装和环境配置

    1. java ee sdk 最新版下载地址 Java EE软件开发包(Software Development Kit, SDK) http://www.oracle.com/technetwork ...

  2. Spring mvc 返回json包含双引号问题 解决

    解决方式1: @RequestMapping(value="/shopsList.json", produces = "text/html;charset=UTF-8&q ...

  3. ubuntu编译安装php7, 安装openssl

    sudo apt-get install openssl sudo apt-get install libssl-dev

  4. heat 用法 示例

    heat.exe dir "./SampleFolder" -cg Files -dr INSTALLDIR -gg -g1 -sf -srd -var "var.Tar ...

  5. MonoDevelop 设置

    菜单:Tools->Options Text Editor Behavior Automatic behaviors × enable on the fly code formatting Sy ...

  6. Docker之My sql 之旅

    要用到mysql 数据库,本来想在本机装,后来想想还是有点污染环境,既然有docker为什么不用呢? 于是开启了采坑之旅,与其说采坑,倒不如说看文档不仔细看. docker pull mysql:5. ...

  7. Numpy 基础知识

    1.使用ipython --pylab 自动加载 Numpy.Scipy.Matplotlib模块. 创建数组a = arange(10) b = arange(10,dtype='f')c = ar ...

  8. Gson转换时,Double转式化

    package com.mall.core; import java.lang.reflect.Type; import java.text.DecimalFormat; import com.goo ...

  9. 有名管道FIFO

    管道和FIFO的特征之一是它们的数据是一个字节流.这是UNIX的原生I/O模型.进程往其中写入的是字节流,系统不对它作解释. FIFO不存数据,只是通过它找到内核文件. 一.建立有名管道 1.命令mk ...

  10. redis的五种常见数据类型的常用指令

      一.String字符串,key-value 应用场景:string是redis的最基本数据类型,key-value格式,一个key对应一个值的情况下 1.设置key = value:set key ...