[Minimum fee, maximum flow + adjacent table] poj 2135

Source: Internet
Author: User

because there are two different paths, the architecture can be as follows: Super Source Vertex S-> 1, capacity is 2, edge weight is 0, N-> super sink vertex t, the capacity is 2, the edge weight is 0, the capacity of other points is 1, and the edge weight is the input data. This ensures that there must be two different paths, and the model must be full, because this question has a duplicate edge, you cannot use an adjacent matrix. You can only use an adjacent table.

# Define INF 1 <30 # define n 10050 struct node {int U, V, Cap, W; int next;} e [N * 6]; int head [N/10]; int pre [N/10]; int dis [N/10]; bool vis [N/10]; int minc; int ecnt; int min (int A, int B) {return A> B? B: A;} void Init () {ecnt = 0; memset (Head,-1, sizeof (head);} void add (INT U, int V, int cap, int W) {e [ecnt]. U = u; E [ecnt]. V = V; E [ecnt]. CAP = CAP; E [ecnt]. W = W; E [ecnt]. next = head [u]; head [u] = ecnt ++; E [ecnt]. U = V; E [ecnt]. V = u; E [ecnt]. CAP = 0; E [ecnt]. W =-W; E [ecnt]. next = head [v]; head [v] = ecnt ++;} void spfa (int s, int t) {queue <int> QQ; while (! QQ. empty () QQ. pop (); int I, j; while (1) {for (I = s; I <= T; I ++) {vis [I] = 0; dis [I] = inf; Pre [I] =-1; // here !!! Initialization !!! Record path !!! Adjacent table !!!} Dis [s] = 0; QQ. Push (s); vis [s] = 1; while (! QQ. empty () {int u = QQ. front (); QQ. pop (); vis [u] = 0; For (INT I = head [u]; I! =-1; I = E [I]. next) {int v = E [I]. v; If (E [I]. cap & dis [u] + E [I]. W <dis [v]) {dis [v] = dis [u] + E [I]. w; Pre [v] = I; If (! Vis [v]) {vis [v] = 1; QQ. push (v) ;}}}if (pre [T] =-1) {return;} int A = inf; int K = pre [T]; while (K! =-1) {A = min (A, E [K]. CAP); k = pre [E [K]. u];} k = pre [T]; while (K! =-1) {e [K]. cap-= A; E [k ^ 1]. cap + = A; // returns the ID of the reverse edge K = pre [E [K]. u];} minc + = dis [T] * A;} return;} int main () {int n, m; while (scanf ("% d ", & N, & M )! =-1) {int I, j; Init (); While (M --) {int U, V, W; scanf ("% d ", & U, & V, & W); add (u, v, 1, W); add (v, U, 1, W);} Add ); add (n, n + 1, 2, 0); int S = 0, T = n + 1; minc = 0; spfa (S, T ); printf ("% d \ n", minc);} 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.