UVALive-5095 Transportation (split + cost flow)

Source: Internet
Author: User

Main topic: There are n points, M edge, each side of the capacity of CI, the cost of ai* x^2 (x for flow, ai for the coefficient)
Now ask if you can transport K units of goods from point 1 to N, and the minimum cost

Problem-solving ideas: Split the edge, split each side into CI bar, each side of the cost of AI * 1, ai * 3, AI * 5 ... Capacity is 1, in the same capacity, you will choose a low-cost stream, so that the flow of the edge up to the cost of just AI * traffic ^2

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace STD;#define N 1010#define INF 0x3f3f3f3fstructedge{intFrom, to, caps, flow, cost; Edge () {} Edge (intFromintTo,intCapintFlowintCost): From, to, Cap (CAP), flow, cost (cost) {}};structmcmf{intN, m, source, sink;intflow, cost; vector<Edge>Edges vector<int>G[n];intD[n], F[n], p[n];BOOLVis[n];voidInitintN) { This->n = n; for(inti =0; I <= N;        i++) g[i].clear ();    Edges.clear (); }voidAddedge (intFromintTo,intCapintCost) {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); }BOOLBellmanford (intSintTint&flow,int&cost,intNeed) { for(inti =0; I <= N; i++) D[i] = INF;memset(Vis,0,sizeof(VIS)); Vis[s] =1; D[s] =0; F[s] = need; P[s] =0; Queue<int>Q; Q.push (s); while(! Q.empty ()) {intU = Q.front ();            Q.pop (); Vis[u] =0; for(inti =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]; F[e.to] = min (F[u], e.cap-e.flow);if(!vis[e.to]) {Vis[e.to] =true;                    Q.push (e.to); }                }            }        }if(D[t] = = INF)return false;        Flow + = F[t]; Cost + = D[t];intu = t; while(U! = s)            {Edges[p[u]].flow + = f[t]; Edges[p[u] ^1].flow-= f[t];        u = edges[p[u]].from; }return true; }voidMincost (intSintTintNeed) {flow =0, cost =0; while(Bellmanford (S, t, flow, cost, Need-flow)) {if(need = = flow) Break; }    }}; MCMF MCMF;intN, M, K;intnum[6] = {0,1,3,5,7,9};voidSolve () {intU, V, a, C; Mcmf.init (n); for(inti =0; I < m; i++) {scanf("%d%d%d%d", &u, &v, &a, &c); for(intj =1; J <= C; J + +) MCMF. Addedge (U, V,1, Num[j] * a); } MCMF. Mincost (1, N, K);if(Mcmf.flow = = k)printf("%d\n", mcmf.cost);Else        printf(" -1\n");}intMain () { while(scanf("%d%d%d", &n, &m, &k)! = EOF) solve ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVALive-5095 Transportation (split + cost flow)

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.