Hdu4240: Find the path with the highest traffic/(the algorithm above 90% on the internet is incorrect)

Source: Internet
Author: User

Question: Find the maximum flow/the maximum flow. (Data water on HDU .)

Train of Thought 1; update the traffic every time you increase the bandwidth to save the largest traffic. Error: during each update, the maximum traffic may be the one that has been extended several times (multiple lines are added each time), so that the biggest one is broken down. This is the case with code 20% on the Internet. (The second data below is 2 or 3)

Idea 2: update the current maximum capacity of each edge each time you increase the value. It seems okay. Incorrect: The maximum traffic may also be killed by traffic delivery (data 4 ).

My solution: since we can't be lazy (maybe we didn't think of how to maintain it by means of augmented time), we can search for the maximum traffic by using the DFS pre-search.

Data:

4
1 6 7 0 5
0 1 4
1 4 4
4 5 4
0 2 3
2 5 3
0 3 3
3 5 3


2 6 6 0 5
0 1 5
1 2 5
2 3 5
3 4 4
4 5 5
0 4 2


3 6 6 0 5
0 1 70
1 2 40
2 4 40
1 3 20
3 4 20
4 5 50


4 7 8 1 5
1 2 5
2 3 2
3 5 2
2 4 3
4 5 3
2 6 5
6 7 5
7 5

Answer: 2.500 1.250 1.250 1.000

# Include <iostream> # include <queue> # include <cstdio> # include <cstring> # include <string> using namespace STD; const int INF = 0x3f3f3f3f; const int maxv = 1002, MaxE = 400000; int nume = 0; int head [maxv]; int e [MaxE] [3]; void inline Adde (int I, Int J, int c) {e [nume] [0] = J; E [nume] [1] = head [I]; head [I] = nume; E [nume ++] [2] = C; E [nume] [0] = I; E [nume] [1] = head [J]; head [J] = nume; E [nume ++] [2] = 0;} int SS, TT, n, m; int vis [maxv]; int lev[ maxv ]; Bool BFS () {for (INT I = 0; I <maxv; I ++) vis [I] = lev[ I] = 0; queue <int> q; q. push (SS); vis [ss] = 1; while (! Q. Empty () {int cur = Q. Front (); q. Pop (); For (INT I = head [cur]; I! =-1; I = E [I] [1]) {int v = E [I] [0]; If (! Vis [v] & E [I] [2]> 0) {lev[ v] = lev[ cur] + 1; vis [v] = 1; q. push (v) ;}} return vis [TT];} int minmaxf = 0; int DFS (int u, int minf) {/* If (u = TT) // use the updated method at the beginning, and then think about the error... {If (mins> minmaxf) minmaxf = mins;} */If (u = TT | minf = 0) return minf; int sumf = 0, F; for (INT I = head [u]; I! =-1 & minf; I = E [I] [1]) {int v = E [I] [0]; if (lev_v] = lev_u] + 1 & E [I] [2]> 0) {f = DFS (V, minf <E [I] [2]? Minf: E [I] [2]); e [I] [2]-= f; E [I ^ 1] [2] + = f; sumf + = F; minf-= f ;}} if (! Sumf) lev[ u] =-1; return sumf;} int dinic () {int sum = 0; while (BFS () sum + = DFS (SS, INF ); return sum;}; void dfsget (int u, int mins) // pre-process the maximum traffic {If (mins <minmaxf) return; If (u = TT) {If (mins> minmaxf) minmaxf = mins ;}for (INT I = head [u]; I! =-1; I = E [I] [1]) {int v = E [I] [0]; If (vis [v] = 0) {vis [v] = 1; dfsget (v, Mins <E [I] [2]? Mins: E [I] [2]); vis [v] = 0 ;}}void read_build () {int AA, BB, CC; For (Int J = 0; j <m; j ++) {scanf ("% d", & aa, & BB, & CC); Adde (AA, BB, CC );} /* For (INT I = 0; I <= tt; I ++) for (Int J = head [I]; J! =-1; j = E [J] [1]) {printf ("% d-> % d: % d \ n", I, E [J] [0], E [J] [2]);} */} void Init () {nume = 0; For (INT I = 0; I <= maxv; I ++) {vis [I] = 0; head [I] =-1;} minmaxf = 0;} int main () {int T; scanf ("% d", & T); For (int ii = 1; II <= T; II ++) {int TX; scanf ("% d", & Tx, & N, & M, & SS, & TT); Init (); read_build (); vis [ss] = 1; dfsget (SS, INF); int ans = dinic (); double Bi = ans * 1.0/minmaxf; printf ("% d %. 3lf \ n ", II, Bi);} return 0 ;}




Related Article

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.