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.

思路:建立源点为A,汇点为Z的网络跑最大流

注意读入格式

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm> using namespace std; const int maxn=1e4 + ;
const int inf=0x3f3f3f3f; template<class T>inline void read(T &res)
{
char c;T flag=;
while((c=getchar())<''||c>'')if(c=='-')flag=-;res=c-'';
while((c=getchar())>=''&&c<='')res=res*+c-'';res*=flag;
} struct edge{int from,to,cap,flow;}; struct isap
{
int n,s,t,p[maxn],d[maxn],cur[maxn],num[maxn];
bool vis[maxn];
vector<int>g[maxn];
vector<edge>edges;
void init(int n,int s,int t) {
this->n = n;
this->s = s;
this->t = t;
for(int i = ;i <= n;i++) g[i].clear();
edges.clear();
}
void addegde(int from,int to,int cap) {
edges.push_back((edge){from, to, cap, });
edges.push_back((edge){to, from, , });
int m = edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
} int augment() {///找增广路
int x = t,a = inf;
while(x!=s) {
a = min(a, edges[p[x]].cap - edges[p[x]].flow);
x = edges[p[x]].from;
}
x=t;
while(x != s) {
edges[p[x]].flow += a;
edges[p[x]^].flow = -a;
x = edges[p[x]].from;
}
return a;
}
int maxflow() {///更新最大流
int flow = ;
memset(num, , sizeof(num));
memset(cur, , sizeof(cur));
for(int i = ; i <= n; i++) num[d[i]]++;
int x = s;
while(d[s] < n) {///最长的一条链上,最大的下标是nv-1,如果大于等于nv说明已断层
if(x == t) {
flow += augment();
x = s;//回退
}
bool ok = ;
for(int i = cur[x]; i < g[x].size(); i++) {
edge &e = edges[g[x][i]];
if(d[x] == d[e.to] + && e.cap > e.flow) {
p[e.to] = g[x][i];
cur[x] = i;x = e.to;
ok = ;
break;
}
}
if(!ok) {
int m = n-;
for(int i = ; i < g[x].size();i++) {
edge &e=edges[g[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
num[d[x]]--;
if(!num[d[x]]) break;
d[x] = m+;
num[d[x]]++;
cur[x] = ;
if(x != s) x = edges[p[x]].from;
}
}
return flow;
}
}ISAP; int main()
{
int n,m,s,t,u,v,w;
n = ;
s = ,t = ;
cin >> m;
ISAP.init(n,s,t);
for(int i = ; i <= m; i++) {
char u,v;
int c;
cin >> u >> v >> c;
ISAP.addegde(u - 'A' + ,v - 'A' + ,c);
}
printf("%d\n",ISAP.maxflow());
return ;
}

[USACO09JAN]Total Flow【网络流】的更多相关文章

  1. [USACO09JAN]Total Flow

    OJ题号: BZOJ3996.洛谷2936.SPOJ-MTOTALF.SCU3353 思路: 题目的要求是将所有边合并成一条边,求合并后的流量. 实际上相当于直接求最大流. EdmondsKarp模板 ...

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

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

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

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

  4. [USACO09JAN]全流Total Flow

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

  5. 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 < ...

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

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

  7. BZOJ3396: [Usaco2009 Jan]Total flow 水流

    3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 27[Sub ...

  8. 3396: [Usaco2009 Jan]Total flow 水流

    3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 179  Solved: 73[Su ...

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

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

随机推荐

  1. Spark存储介绍

    目录 整体架构 存储相关类 应用启动时 增删改后更新元数据 获取数据存放位置 数据块的删除 RDD存储调用 数据读取 数据写入 cache & checkpoint Reference 记录一 ...

  2. 基于SSM开发自行车在线租赁管理系统源码

    开发环境: Windows操作系统开发工具: Myeclipse+Jdk+Tomcat+MYSQL数据库注意:此项目分管理员与普通用户两种角色运行效果图 源码及原文链接:https://javadao ...

  3. 常用js封装

    //获取url参数 function getUrlParams(name, url) { if (!url) url = location.href; name = name.replace(/[\[ ...

  4. maven导入sqlserver驱动jar包依赖包到本地仓库

    maven导入sqlserver驱动jar包依赖包到本地仓库 maven项目使用sqlserver的依赖,先下载一个sqlserver的驱动,网址:https://www.microsoft.com/ ...

  5. MySQL存储过程和游标

    一.存储过程 什么是存储过程,为什么要使用存储过程以及如何使用存储过程,并且介绍创建和使用存储过程的基本语法. 什么是存储过程: 存储过程可以说是一个记录集,它是由一些T-SQL语句组成的代码块,这些 ...

  6. JS数据类型和堆栈+变量比较和值的复制+参数传递和类型检测

    变量命名 变量名:字母 数字 下划线 美元符$ jquery:  $     $.each()   $ === jQuery underscore( js的一个函数库)  :   _     _.ea ...

  7. Qt的QString,QByteArray,char *相互转换

    1.QString转换为QByteArray QString str = "; QByteArray byte = str.toUtf8(); // 转换为Utf8格式 byte.toLoc ...

  8. MyBatis中foreach循环的用法

    一.在了解foreach之前,先了解一下mybatis传入参数及parameterType 1.我们在Dao层向对应的mapper.xml文件传递参数时,可以传递的参数有: ①.基本数据类型(如int ...

  9. 灵活运用SQL Server2008 SSIS变量

      在SSIS开发ETL(Extract-Transform-Load),数据抽取.转换.装载的过程.我们需要自己定义变量 一.SSIS变量简介 SSIS(SQL Server Integration ...

  10. phpcmsv9 后台统计编辑发稿数量

    直切正题: 每个人,每个栏目,发稿数量统计 SELECT a.realname AS 姓名, c.catname AS 栏目名称, count(1) AS 发稿量FROM v9_news bz, v9 ...