SOJ3353 Total Flow of problem solving report
Description
Time limit:2000 MS Memory limit:65536 kthe Problemproblem name:flow
Farmer John always wants he cows to having enough water and thus has made a map of the N (1 <= n <=) water pipes On the farm that's connect the well to the barn. He is surprised to find a wild mess of different size pipes connected in an apparently haphazard. He wants to calculate the flow through the pipes.
Pipes connected in a row allow water flow This is the minimum of the values of the "flow values". The example of a pipe with flow capacity 5 connecting to a pipe of flow capacity 3 can is reduced logically to a single pi PE of flow capacity 3:
+---5---+---3---+ +---3---+
Similarly, pipes in parallel let through water this is the sum of their flow capacities:
+---5---+
---+----> +---8---+
+---3---+
Finally, a pipe that connects to nothing else can is removed; It contributes no flow to the final overall capacity:
+---5---+
---+ +---3---+
+---3---+--
All the pipes in the many mazes of plumbing can is 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 is 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 the legs of BZ can be combined:
B
A +---3---+---9---+z
Then AB and BZ can is combined to yield a net capacity of 3:
A +---3---+z
Write a program to read in a set of pipes described as, endpoints and then calculate the net flow capacity from ' a ' to ' Z '. All networks in the test data can is reduced using the rules here.
Pipe I connects II different nodes a_i and b_i (a_i in range ' a-za-z '; b_i in range ' a-za-z ') and have flow f_i (1 <= F _i <= 1,000). Note that Lower-and upper-case node names is intended to be treated as different.
The system would provide extra test case feedback for your first submissions.
INPUT format:* Line 1: A single integer:n
* Lines 2..N + 1:line i+1 describes pipe I with II letters and an integer, all space-separated:a_i, b_i, and F_i
The input contains multiple test cases.
SAMPLE Input:5
A B 3
B C 3
C D 5
D Z 4
B Z 6
OUTPUT format:* Line 1: A single integer, the maximum flow from the well (' A ') to the Barn (' Z ')
SAMPLE Output:3
The main idea: Also a shameful reading comprehension, give you n a water pipe connection way and flow, ' A ' for the water, ' Z ' for the barn, ask you the maximum flow of water to the barn is how much?
Analysis: A direct look at the sample found to be bare maximum flow. The processing of letters can be directed to the ASCII code to the node number (note that the node to leave enough), the source point is ' A ' and the meeting point is ' Z '. There is no difficulty is a template problem.
on the code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < queue>using namespace Std;const int maxn = 210;const int MAXM = 10000;const int INF = 0x3f3f3f3f;struct Edge{int from, To, next, cap;}; Edge edge[maxm];int level[maxn];int head[maxn];int src, des, cnt;void addedge (int from, int. to, int cap) {Edge[cnt].from = From;edge[cnt].to = To;edge[cnt].cap = Cap;edge[cnt].next = Head[from];head[from] = Cnt++;swap (from, to); Edge[cnt].fro m = from;edge[cnt].to = To;edge[cnt].cap = 0;edge[cnt].next = Head[from];head[from] = cnt++;} int BFs () {memset (level,-1, sizeof level), CNT = 0;queue<int> q;while (!q.empty ()) Q.pop (); LEVEL[SRC] = 0;q.push (sr c); while (!q.empty ()) {int u = q.front (), Q.pop (); for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (E Dge[i].cap > 0 && level[v] = =-1) {Level[v] = Level[u] + 1;q.push (v);}}} return Level[des]! =-1;} int dfs (int u, int f) {if (U = = des) return f;int tem;for (int i= Head[u]; I! =-1; i = edge[i].next) {int v = edge[i].to;if (Edge[i].cap > 0 && level[v] = = Level[u] + 1) {tem = DFS (V, min (f, EDG E[I].CAP)); if (Tem > 0) {edge[i].cap-= Tem;edge[i^1].cap + Tem;return tem;}}} Level[u] = -1;return 0;} int dinic () {int ans = 0, Tem;while (BFS ()) {while (tem = DFS (src)) > 0) {ans + = tem;}} return ans;} int main () {int n;src = ' A ', des = ' Z ', while (CIN >> N) {memset (head,-1, sizeof head), CNT = 0;char A, B;int c;for ( int i = 1; I <= N; i++) {cin >> a >> b >> C;addedge (A, B, c);//Leave the number of nodes sufficient, directly using the ASCII code of the character as the number}cout << dinic () << Endl ;} return 0;}
SOJ3353 Total Flow of Problem solving report