10338: 洛谷P2936 - [USACO09JAN] Total Flow S
[Creator : ]
Description
Farmer John always wants his cows to have enough water and thus has made a map of the $N\ (1 \leq N \leq 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$:
Similarly, pipes in parallel let through water that is the sum of their flow capacities:
Finally, a pipe that connects to nothing else can be removed; it contributes no flow to the final overall capacity:
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:
Pipe BC and CD can be combined:
Then BD and DZ can be combined:
Then two legs of BZ can be combined:
Then AB and BZ can be combined to yield a net capacity of $3$:
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 \leq F_i \leq 1,000)$. Note that lower- and upper-case node names are intended to be treated as different.
约翰总希望他的奶牛有足够的水喝,因此他找来了农场的水管地图,想算算牛棚得到的水的总流量。农场里一共有 $N$ 根水管。约翰发现水管网络混乱不堪,他试图对其进行简化。他简化的方式是这样的:
- 两根水管串联,则可以用较小流量的那根水管代替总流量。
- 两根水管并联,则可以用流量为两根水管流量和的一根水管代替它们。
当然,如果存在一根水管一端什么也没有连接,可以将它移除 。
请写个程序算出从水井 $\text{A}$ 到牛棚 $\text{Z}$ 的总流量。数据保证所有输入的水管网络都可以用上述方法简化 。
注意:输入数据中每个顶点用大写字母或者小写字母表示。也就是说,输入里可能出现用小写字母命名的节点。
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 \leq F_i \leq 1,000)$. Note that lower- and upper-case node names are intended to be treated as different.
约翰总希望他的奶牛有足够的水喝,因此他找来了农场的水管地图,想算算牛棚得到的水的总流量。农场里一共有 $N$ 根水管。约翰发现水管网络混乱不堪,他试图对其进行简化。他简化的方式是这样的:
- 两根水管串联,则可以用较小流量的那根水管代替总流量。
- 两根水管并联,则可以用流量为两根水管流量和的一根水管代替它们。
当然,如果存在一根水管一端什么也没有连接,可以将它移除 。
请写个程序算出从水井 $\text{A}$ 到牛棚 $\text{Z}$ 的总流量。数据保证所有输入的水管网络都可以用上述方法简化 。
注意:输入数据中每个顶点用大写字母或者小写字母表示。也就是说,输入里可能出现用小写字母命名的节点。
Input
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, F_i$
Lines 2..N + 1: Line $i+1$ describes pipe i with two letters and an integer, all space-separated: $a_i, b_i, F_i$
Output
Line 1: A single integer that the maximum flow from the well ('A') to the barn ('Z')
Sample 1 Input
5
A B 3
B C 3
C D 5
D Z 4
B Z 6
Sample 1 Output
3