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. jmeter java请求:java.lang.VerifyError: Cannot inherit from final class

    被这个问题block了一天,应该是包冲突的.通过对包删减排查,结果发现是netty-all-xxx.Final.jar包的问题 应该是jmeter版本和netty版本的冲突吧,换成jmeter 3.1 ...

  2. 进程和创建线程的两种方法(threading.Thread)

    进程 如QQ 要以一个整体的形式暴露给操作系统管理,里面包含对各种资源的调用,内存的管理, 网络接口的调用等,进程就是各种资源管理的集合 线程:是操作系统最小的调度单位,是一串指令的结合 进程 要操作 ...

  3. Maven 项目中 添加自己的jar包

    mvn install:install-file   -Dfile=java-bloomfilter-1.0.jar  -DgroupId=com.sina  -DartifactId=java-bl ...

  4. python中关键字的总结

    python中各种关键字的总结:用表格形式,解释关键字符号的作用和案例说明                    关键字                                         ...

  5. RISC处理器

     RISC(精简指令集算法)处理器是经过硬件的精简只执行很有限的最常用的那部分指令的处理器.因为通过研究发现,只有 大约 20%的指令是最常用的,把处理器能执行的指令数目减少到 最低限度,对它们的执行 ...

  6. COM组件三大接口IUnknown、IClassFactory、IDispatch。

    转自:http://blog.csdn.net/chenyujing1234/article/details/7753863 (1)COM组件有三个最基本的接口类,分别是IUnknown.IClass ...

  7. hibernate中.常见的hql查询语句

    hql是非常有意识的被设计为完全面向对象的查询 基本规则: 1.hql语法类似于sql,但它后面跟的不是表名和字段名,而是类名和属性名 2.hql大小写不敏感.但是设计java类名,包名,属性名时大小 ...

  8. 疯狂JAVA——第四章 流程控制与数组

    4.5 数组类型 数组也是一种类型.它本身是引用类型.例如int是一种基本类型,int[]就是引用类型. 两种定义数组的方式: 1.type[] arrayName; 2.type arrayName ...

  9. 转-git 配置用户名和邮箱

    原址:http://www.cnblogs.com/fsong/p/5540840.html 在安装了git for windows之后,个人总是忘记配置git config的命令,以此记录一下: 配 ...

  10. goim源码分析与二次开发-comet分析二

    这篇就是完全原版了,作为一个开始,先介绍comet入口文件main.go 第一步是初始化配置,还有白名单.还有性能监口,整体来说入口代码简洁可读性很强 然后开始初始化监控,还有bukcet这里buck ...