HDU 3667-- Transportation【最小費最大流 && 拆邊建圖 && 經典】,hdu3667--

來源:互聯網
上載者:User

HDU 3667-- Transportation【最小費最大流 && 拆邊建圖 && 經典】,hdu3667--

TransportationTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2441    Accepted Submission(s): 1041


Problem DescriptionThere are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry, the more dangerous it is. To be 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 is an upper bound Ci, which means that you cannot transport more than Ci units of goods along this road. Please note you can only carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.
 
InputThere are several test cases. The first line of each case contains three integers, N, M and K. (1 <= N <= 100, 1 <= M <= 5000, 0 <= K <= 100). Then M lines followed, each contains four integers (ui, vi, ai, Ci), indicating there is a directed road from city ui to vi, whose coefficient is ai and upper bound is Ci. (1 <= ui, vi <= N, 0 < ai <= 100, Ci <= 5) 
OutputOutput one line for each test case, 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
 

題意:

有N個城市和M條單向路線。對於每條線路,有四個資訊:起點a、終點b、費用係數c、容量d(1<=d<=5),表示(1) 運送x單位的貨物需要花費c * x * x,(二) 這條路最多能運送d單位的貨物。現在問你能否將K單位貨物從1運送到N,若可以輸出最小花費,否則輸出-1。


解析:

這題一看就是用費用流來寫,但是費用流的前提是費用和流量是成正比的,但這一題費用和流量的平方成正比。所以這裡要轉化一下,目的是要把c * x * x 變成 c * x1 + c* x2 + ...c * xi。即把每條邊拆d條邊,每條邊的費用為 c * xi,容量為1。這裡我們就要想辦法表示出xi。

根據等差公式,我們知道,n * n = 1 + 3 + 5 + … +(2n - 1)。 這就是我們要表示的xi。

轉化好以後就是裸的費用流的問題了。

不太理解的可以看看這篇文章,講的比我仔細。點我


#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;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.