[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 

思路:最大流

代码实现:

 1 #include<cstdio>
 2 #include<cstring>
 3 #define inf 100000000
 4 int n,s,t,tw;
 5 int a,b,c;
 6 char ch[3],cn[3];
 7 int h[300],hs=1;
 8 struct edge{int s,n,w;}e[60000];
 9 int d[300],q[300],head,tail;
10 inline int min(int x,int y){return x<y?x:y;}
11 void bfs(){
12     memset(d,0,sizeof(d));
13     head=tail=0;
14     d[s]=1,q[head++]=s;
15     while(head>tail){
16         a=q[tail++];
17         for(int i=h[a];i;i=e[i].n)
18         if(!d[e[i].s]&&e[i].w){
19             d[e[i].s]=d[a]+1;
20             if(e[i].s==t) return;
21             q[head++]=e[i].s;
22         }
23     }
24 }
25 int ap(int k,int w){
26     if(k==t) return w;
27     int uw=w;
28     for(int i=h[k];i&&uw;i=e[i].n)
29     if(e[i].w&&d[e[i].s]==d[k]+1){
30         int wt=ap(e[i].s,min(uw,e[i].w));
31         if(wt) e[i].w-=wt,e[i^1].w+=wt,uw-=wt;
32         else d[e[i].s]=0;
33     }
34     return w-uw;
35 }
36 bool Dinic(){
37     bfs();
38     if(!d[t]) return 0;
39     tw+=ap(s,inf);
40     return 1;
41 }
42 int main(){
43     scanf("%d",&n);
44     s=‘A‘,t=‘Z‘;
45     while(n--){
46         scanf("%s%s%d",ch,cn,&c);
47         a=ch[0],b=cn[0];
48         e[++hs]=(edge){b,h[a],c},h[a]=hs;
49         e[++hs]=(edge){a,h[b],c},h[b]=hs;
50     }
51     while(Dinic());
52     printf("%d\n",tw);
53     return 0;
54 }

我终于能顺手的,顺手A了,网络流真心好实现。

题目来源:洛谷

时间: 2024-10-25 16:50:40

[USACO09JAN]全流Total Flow的相关文章

洛谷 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 appa

洛谷——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 appa

bzoj3396[Usaco2009 Jan]Total flow 水流*

bzoj3396[Usaco2009 Jan]Total flow 水流 题意: 求无环图的最大流.边数≤700. 题解: 管它有没有环.注意本题的节点标号既有大写字母,也有小写字母. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #define inc(i,j,k) for(int i=j;i<=k;i++) 6 #de

解题报告 之 SOJ3353 Total Flow

解题报告 之 SOJ3353 Total Flow Description Time Limit: 2000 MS Memory Limit: 65536 K The Problem PROBLEM NAME: 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 conne

3396: [Usaco2009 Jan]Total flow 水流

3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 179  Solved: 73[Submit][Status] Description Input 第1行输入N,之后N行每行描述一条水管,前两个英文字母表示水管的两端(大小写字母是不一样的),后一个整数表示水管的流量,流量不会超过1000. Output 一个整数,表示总流量. Sample Input 5 A B 3 B C 3

Openvswitch原理与代码分析(5): 内核中的流表flow table操作

? 当一个数据包到达网卡的时候,首先要经过内核Openvswitch.ko,流表Flow Table在内核中有一份,通过key查找内核中的flow table,即可以得到action,然后执行action之后,直接发送这个包,只有在内核无法查找到流表项的时候,才会到用户态查找用户态的流表.仅仅查找内核中flow table的情况被称为fast path. ? ? 第一步:从数据包中提取出key ? 实现函数为int ovs_flow_key_extract(const struct ip_tun

洛谷 P3128 [USACO15DEC]最大流Max Flow

P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1…N. Each pipe connects a pair of stalls,

最大流----F - Flow Problem

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define N 1e9 using namespace std; queue<int>q; int mapp[200][200]; int flow[200][200]; int a[200]; int p[200]; int n,m; i

洛谷P3128 [USACO15DEC]最大流Max Flow

洛谷P3128 [USACO15DEC]最大流Max Flow 题目描述 FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N.所有隔间都被管道连通了. FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti.一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少. 输入输出格式 输入格式: The first line of the input contains