HDU 3667--transportation "minimum fee max Flow && split build && classic"

Source: Internet
Author: User

TransportationTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2441 Accepted Submission (s): 1041


Problem Descriptionthere is N cities, and M directed roads connecting them. Now the want to transport K units of the goods from the City 1 to the city N. There is many robbers on the road and so you must be very careful. The more goods your carry, the more dangerous it is. To being more specific, for each road I, there is a coefficient ai. If you want to carry X units of goods along this road, you should pay AI * x2 dollars to hire guards to protect your goods . And what's worse, for each road I, there are an upper bound Ci, which means so you cannot transport more than Ci units of Goods along this road. Please note that you can be carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.

Inputthere is several test cases. The first line of all case contains three integers, N, M and K. (1 <= N <=, 1 <= M <=, 0 <= K &lt ; = 100). Then M. Lines followed, each contains four integers (UI, VI, AI, Ci), indicating there are a directed road from the city UI to V I, whose coefficient is AI and upper bound are Ci. (1 <= UI, vi <= N, 0 < ai <=, Ci <= 5)
Outputoutput one, indicating the minimum cost. If It is impossible to transport all the K units of goods, output-1.


Sample Input
2 1 21 2 1 22 1 21 2 1 12 2 21 2 1 21 2 2 2

Sample Output
4-13

Test instructions

There are n cities and m one-way routes. For each line, there are four messages: The starting point A, the end point B, the cost factor C, the capacity D (1<=d<=5), indicating that (1) the shipment of x units costs C * x * x, (ii) This road can transport up to D units of cargo. Now ask if you can transport K Unit goods from 1 to N, if you can output the minimum cost, otherwise output-1.


Analytical:

This question is to use the cost flow to write, but the premise of the cost flow is proportional to the cost and flow, but the cost of the problem and the flow of the square is proportional. So here's the conversion, the purpose is to turn c * x * x into C * x1 + c* x2 + ... c * XI. That is, each side of the D-bar, each side of the cost of C * XI, the capacity of 1. Here we will find a way to show XI.

According to the linear formula, we know that n * n = 1 + 3 + 5 + ... + (2n-1). This is the XI we are going to show.

After the transformation is the bare cost flow problem.

Don't quite understand can look at this article, speak more carefully than me. Point Me


#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 110# Define MAXM 100000 + 10#define INF 0x3f3f3f3fusing namespace Std;int N, M, k;int inset, outset;struct node {int u, V, CAP, flow, cost, next;};    Node Edge[maxm];int HEAD[MAXN], Cnt;int DIST[MAXN], vis[maxn];int per[maxn];void init () {cnt = 0; Memset (Head,-1, sizeof (head));}    void Add (int u, int v, int w, int c) {edge[cnt].u = u;    EDGE[CNT].V = v;    Edge[cnt].cap = W;    Edge[cnt].flow = 0;    Edge[cnt].cost = C;    Edge[cnt].next = Head[u];    Head[u] = cnt++;    edge[cnt].u = v;    EDGE[CNT].V = u;    Edge[cnt].cap = 0;    Edge[cnt].flow = 0;    Edge[cnt].cost =-C;    Edge[cnt].next = Head[v]; HEAD[V] = cnt++;}    void Getmap () {int A, B, C, D;    outset = 0;    Inset = n + 1;        for (int i = 1;i <= m; ++i) {scanf ("%d%d%d%d", &a, &b, &c, &d);        for (int j = 1; j <= D; ++j) {Add (A, B, 1, (J * 2-1) *c);  }    }  Add (outset, 1, K, 0); Add (n, inset, K, 0);}    BOOL SPFA (int st, int ed) {queue<int>q;        for (int i = 0; I <= inset; ++i) {dist[i] = INF;        Vis[i] = 0;    Per[i] =-1;    } Dist[st] = 0;    VIS[ST] = 1;    Q.push (ST);        while (!q.empty ()) {int u = q.front ();        Q.pop ();        Vis[u] = 0;            for (int i = head[u]; i =-1; i = Edge[i].next) {node E = Edge[i];                if (DIST[E.V] > Dist[u] + e.cost && e.cap > E.flow) {dist[e.v] = Dist[u] + e.cost;                PER[E.V] = i;                    if (!vis[e.v]) {VIS[E.V] = 1;                Q.push (E.V); }}}} ' return per[ed]! =-1;}    void MCMF (int st, int ed, int &cost, int &flow) {flow = 0;    Cost = 0;        while (SPFA (St, ed)) {int mins = INF;        for (int i = per[ed]; i =-1; i = per[edge[i ^ 1].v]) {mins = min (mins, edge[i].cap-edge[i].flow); } foR (int i = per[ed]; i =-1; i = per[edge[i ^ 1].v]) {edge[i].flow + = mins;            edge[i ^ 1].flow-= mins;        Cost + = Edge[i].cost * mins;    } flow + = mins;        }}int Main () {while (scanf ("%d%d%d", &n, &m, &k)! = EOF) {init ();        Getmap ();        int cost, flow;        MCMF (outset, inset, cost, flow);        if (flow = = k) printf ("%d\n", cost);    else printf (" -1\n"); } return 0;}


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

HDU 3667--transportation "minimum fee max Flow && split build && classic"

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.