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. JS实现拖动效果

    有个问题就是该模块要使用定位,因为有left,top属性使用,绝对定位和相对定位都行,当然你也可使用margin-left,和margin-top这2个属性,替换left,top也是可以得 这样就不用 ...

  2. 【转】Maven中-DskipTests和-Dmaven.test.skip=true的区别

    主要区别是:是否编译测试类 -DskipTests:编译测试类,但不运行 -Dmaven.test.skip=true:不编译.不运行 转自 http://zephiruswt.blog.51cto. ...

  3. phone手机比较

    phone 15年5月3大厂商发布的高端机器 高颜值手机 皓月银 http://www.vmall.com/product/1983.html 冰川白 http://www.vmall.com/pro ...

  4. xe Style

    //注意引用:vcl.themes, vcl.styles, IOutils procedure TForm1.FormCreate(Sender: TObject); var stylename: ...

  5. 基于OpenGL编写一个简易的2D渲染框架-09 重构渲染器-Shader

    Shader 只是进行一些简单的封装,主要功能: 1.编译着色程序 2.绑定 Uniform 数据 3.根据着色程序的顶点属性传递顶点数据到 GPU 着色程序的编译 GLuint Shader::cr ...

  6. UI5-文档-4.18-Icons

    我们的对话框仍然是空的.因为SAPUI5附带了一个包含500多个图标的大图标字体,所以我们将在对话框打开时添加一个图标来问候用户. Preview An icon is now displayed i ...

  7. React 简单介绍

    React 简单介绍 作者 RK_CODER 关注 2014.12.10 17:37* 字数 2516 阅读 55715评论 6喜欢 70 why React? React是Facebook开发的一款 ...

  8. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  9. mysql in 过滤 解决转义问题

    IF(headUser!='',instr(concat(',',headUser,','),concat(',',cr.headUser,',')),TRUE);

  10. lnmp上传文件

    LAMP环境: Linux Mint 16 32bits xfce apache 2.4.6 Ubuntu php 5.5.3 默认www是/var/www,我用符号连接到了/home/tony/ww ...