Poj 3204 (max stream processing)

Source: Internet
Author: User
Ikki's story I-Road Reconstruction

Time limit:2000 ms   Memory limit:131072 K
Total submissions:4830   Accepted:1317

Description

Ikki is the king of a small country-Phoenix, Phoenix is so small that there is only one city that is responsible for the production of daily goods, and uses the road network to transport the goods to the capital. ikki finds that the biggest problem in
The country is that transportation speed is too slow.

Since Ikki was an ACM/ICPC contestant before, he realized that this, indeed, is a maximum flow problem. he coded a maximum flow program and found the answer. not satisfied with the current status of the transportation speed, he wants to increase the transportation
Ability of the nation. the method is relatively simple, Ikki will reconstruct some roads in this transportation network, to make those roads afford higher capacity in transportation. but unfortunately, the country of Phoenix is not so rich in GDP that there
Is only enough money to rebuild one road. Ikki wants to find such roads that if reconstructed, the total capacity of transportation will increase.

He thought this problem for a loooong time but cannot get it. so he gave this problem to frkstyc, who put it in this poj monthly contest for you to solve. can you solve it for Ikki?

Input

The input contains exactly one test case.

The first line of the test case contains two integersN,M(N≤ 500,M≤ 5,000) which represents the number of cities and roads in the country, Phoenix, respectively.

MLines follow, each line contains three IntegersA,B,C, Which means that there is a road from City
ATo CityBWith a transportation capacityC(0 ≤
A
,B<N,C≤ 100). All the roads are directed.

Cities are numbered from 0N−1, the city which can product goods is numbered 0, and the capital is numberedN−1.

Output

You shoshould output one line consisting of only one integer K, Denoting that there are
KRoads, reconstructing each of which will increase the network transportation capacity.

Sample Input

2 10 1 1

Sample output

1

Source

Poj monthly -- 2007.03.04, Ikki

For the moment, this is the number of key edges .... The idea is like this .. The flow in the created graph is increased or decreased when the maximum flow is obtained, therefore, after processing the maximum stream, a forward edge with Zero traffic will appear (the program will ask what to say to the forward edge ), however, an edge similar to 0-> 0-> 0 appears... Therefore, it is excluded that this is not the only side on this road, and the number of other sides with 0 is the desired value. Here, we can search the graph after the maximum stream processing twice, check whether the S source point can reach the edge with the traffic of 0, and whether the T sink point can reach the edge with the traffic of 0 to solve the problem ....

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; # define maxm 100000 # define maxn 550 # define INF int_max # define CC (M, V) memset (M, V, sizeof (M) struct node {int U, V, F, next;} edge1 [maxm], edge2 [maxm]; int head1 [maxn], head2 [maxn], p, P2; int que [maxn], PI, lev[ maxn], cur [maxn]; int vis1 [maxn], vis2 [maxn]; inline void init1 () {P = P2 = 0, CC (head1,-1), CC (head2,-1);} void addedge (Int U, int V, int f) {edge1 [p]. U = u, edge1 [p]. V = V, edge1 [p]. F = F, edge1 [p]. next = head1 [u], head1 [u] = P; edge2 [p]. U = u, edge2 [p]. V = V, edge2 [p]. f = 0, edge2 [p]. next = head2 [u], head2 [u] = P ++; edge1 [p]. U = V, edge1 [p]. V = u, edge1 [p]. f = 0, edge1 [p]. next = head1 [v], head1 [v] = P; edge2 [p]. U = V, edge2 [p]. V = u, edge2 [p]. F = F, edge2 [p]. next = head2 [v], head2 [v] = P ++;} bool BFS (int s, int t) {Int fir, en, U, V, I; memset (lev_, 0, sizeof (lev_)); lev_[ S] = 1, que [0] = s, fir = 0, en = 1; while (fir! = EN) {u = que [FIR ++]; for (I = head1 [u]; I! =-1; I = edge1 [I]. next) if (edge1 [I]. f> 0 & lev[ v = edge1 [I]. v] = 0) {lev[ v] = lev[ u] + 1, que [en ++] = V; If (V = T) {FIR = en; break ;}}return lev[ T];} int dinic (int s, int t) {int U, J, K, IQ, F; int flow = 0; while (BFS (S, T) {memcpy (cur, head1, sizeof (head1); U = s, IQ = 0; while (1) {If (u = T) {for (k = 0, F = inf; k <IQ; k ++) if (edge1 [que [k]. F <F) F = edge1 [que [k]. f, J = K; For (k = 0; k <IQ; k ++) {edge1 [que [k]. f-= F, edge1 [que [k] ^ 1]. F + = f; edge2 [que [k]. F + = F, edge2 [que [k] ^ 1]. f-= f;} flow + = F, u = edge1 [que [IQ = J]. u;} For (j = cur [u]; cur [u]! =-1; j = cur [u] = edge1 [cur [u]. next) if (edge1 [J]. f> 0 & lev[ u] + 1 = lev[ edge1 [J]. v]) break; If (cur [u]! =-1) {que [IQ ++] = cur [u]; u = edge1 [cur [u]. v;} else {If (IQ = 0) break; lev_[ u] =-1; u = edge1 [que [-- IQ]. U ;}} return flow;} void dfs1 (int u) {vis1 [u] = 1; for (INT I = head1 [u]; I! =-1; I = edge1 [I]. next) if (edge1 [I]. f> 0 & vis1 [edge1 [I]. v] = 0) dfs1 (edge1 [I]. v);} void dfs2 (int u) {vis2 [u] = 1; for (INT I = head2 [u]; I! =-1; I = edge2 [I]. next) if (edge2 [I]. f> 0 & vis2 [edge2 [I]. v] = 0) dfs2 (edge2 [I]. v);} int main () {int n, m, I, U, V, F, ans; scanf ("% d", & N, & M ); init1 (); for (I = 1; I <= m; I ++) {scanf ("% d", & U, & V, & F); addedge (u, v, f);} If (dinic (0, n-1) = 0) {printf ("0 \ n ");} else {CC (vis1, 0), CC (vis2, 0); dfs1 (0), dfs2 (n-1); for (I = 0, ANS = 0; I <p; I + = 2) // + = 2 is the focus, because the return side is definitely not 0 if (edge1 [I]. F <= 0 & vis1 [edge1 [I]. u] & vis2 [edge1 [I]. v]) ans ++; printf ("% d \ n", ANS) ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.