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. awk分割字符串

    想从目标字符串中,提取想要的字符,可以用awk命令. 例如: 从<version>1.3.1-SNAPSHOT</version>中提取版本号,则可以用命令:awk -F'[& ...

  2. is not on any development teams

    is not on any development teams 1)账号正在申请中 2)申请成功后的账号? 加了3个账号,都是这样子的. 1:Xcode>Window> "Org ...

  3. AS3 巧用事件api简化鼠标拖动流程

     拖动,按照一般人的定义,拖动就是鼠标按下的时候移动鼠标,这里面有三个过程,分别是按下.移动鼠标和弹起.以stage为例,大家的实现步骤通常如下:(PS:此处不讨论startDrag和stopDrag ...

  4. as3 XML类和XMLList类的区别

    一.XML类和XMLList类的区别       AS3.0中,处理XML主要用到两个主类,XML类和XMLList类,这两个类的很多内容是共通的.应该有人会问,XML和XMLList的区别是什么? ...

  5. 多线程--Java

    多线程: 1.进程和线程 进程是资源分配的最小单位,线程是CPU调度的最小单位. 每个进程的创建都需要系统为其开辟资源内存空间,并发执行的程序在执行过程中分配和管理资源的基本单位,速度和销毁也较慢.进 ...

  6. 吴裕雄 数据挖掘与分析案例实战(10)——KNN模型的应用

    # 导入第三方包import pandas as pd # 导入数据Knowledge = pd.read_excel(r'F:\\python_Data_analysis_and_mining\\1 ...

  7. Linux网络通信

    使用TCP协议的socket 1.网络字节序 由于在主机存储为小端序,网络传输为大端序,并且在网络中需要读取IP号和端口号,所以发送端要将小端序转为大端序,接收端将大端序转为小端序 #include ...

  8. oozie错误:javax.servlet.jsp.el.ELException: variable [***] cannot be resolved

    完整错误: javax.servlet.jsp.el.ELException: variable [compute] cannot be resolved at org.apache.oozie.ut ...

  9. zabbix3.2的server和zabbix-agent2.2怎么监控MySQL的办法

    zabbix官方支持监控MySQL,但直接使用默认的模板是不可用的,还需要经过额外的设置才可以使用.如果只需要对mysql数据库做简单的监控,zabbix自带的模板完全能够满足要求:如果有更高的需求那 ...

  10. 103041000997维护的是周批,按周合并后再考虑最小采购批量、舍入值、然后回写到SAP系统

    描述:103041000997维护的是周批量,但最终没有按周批量来回写数据. 业务逻辑如下: 1.净需求考虑数量按周汇总(也有按日.按3天,具体 要根据物料主数据维护来判断) 2.第1点的结果再加上安 ...