Network Flow cost flow problems

Source: Internet
Author: User

Billing flow is the maximum minimum fee flow.

 

First paste the template on the powder book:

Struct edge {int from, to, Cap, flow, cost; edge (int u, int V, int C, int F, int W): From (u ), to (V), Cap (C), flow (F), Cost (w) {}}; int n, m; vector <edge> edges; vector <int> G [maxn]; int INQ [maxn]; int d [maxn]; int P [maxn]; int A [maxn]; void Init (int n) {// This-> N = N; the original Chinese version of the fan book contains this sentence, but it will obviously report an error. I feel that removing it has no effect... for (INT I = 0; I <n; I ++) g [I]. clear (); edges. clear ();} void addedge (int from, int to, int cap, int cost) {edges. push_back (edge (from, to, Cap, 0, cost); edges. push_back (edge (to, from, 0, 0,-cost); M = edges. size (); G [from]. push_back (m-2); G [to]. push_back (m-1);} bool bellmanford (int s, int T, Int & flow, long & cost) // The queue has been optimized, it feels like spfa {for (INT I = 0; I <n; I ++) d [I] = inf; memset (INQ, 0, sizeof (INQ )); d [s] = 0; INQ [s] = 1; p [s] = 0; A [s] = inf; queue <int> q; q. Push (s); While (! Q. empty () {int u = Q. front (); q. pop (); INQ [u] = 0; For (INT I = 0; I <G [u]. size; I ++) {edge & E = edges [G [u] [I]; If (E. cap> E. flow & D [E. to]> d [u] + E. cost () {d [E. to] = d [u] + E. cost; P [E. to] = G [u] [I]; A [E. to] = min (A [u], E. cap-e.flow); If (! INQ [E. to]) {q. push (E. to); INQ [E. to] = 1 ;}}}if (d [T] = inf) return false; flow + = A [T]; Cost + = (long) d [T] * (long) A [T]; for (INT u = T; u! = S; u = edges [p [u]. from) {edges [p [u]. flow + = A [T]; edges [p [u] ^ 1]. flow-= A [T];} return true;} int mcmf (int s, int T, long & cost) // S: start point; T: Sink point; {int flow = 0; cost = 0; // flow: calculates the largest stream cost: calculates the fee while (bellmanford (S, T, flow, cost )); return flow ;}
View code

 

Exercise: poj2516

 

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.