zoj 3362 Beer Problem 【最大費用最大流 需要判斷最長路值是否為負】,zoj3362
Beer ProblemTime Limit: 2 Seconds Memory Limit: 32768 KB
Everyone knows that World Finals of ACM ICPC 2004 were held in Prague. Besides its greatest architecture and culture, Prague is world famous for its beer. Though drinking too much is probably not good for contestants, many teams took advantage of tasting greatest beer for really low prices.
A new beer producing company Drink Anywhere is planning to distribute its product in several of the n European cities. The brewery is located near Prague, that we would certainly call city number 1. For delivering beer to other cities, the company is planning to use logistics company Drive Anywhere that provides m routes for carrying goods. Each route is described by the cities it connects (products can be transported in either direction), its capacity --- the number of barrels of beer that can be transported along this route each day, and the cost of transporting one barrel of beer along it. To deliver beer to some city it may be necessary (or advantageous) to use several routes consequently, and maybe even deliver beer using several different paths.
Each city is in turn characterized by the price that local pubs and restaurants are ready to pay for one barrel of beer. You may assume that demand for beer is essentially unlimited in each city, since this is the product that will always find its consumer.
Drink Anywhere is not planning to distribute its beer in Prague for a while, because of the high competition there, so it is just planning to provide beer to other cities for now. Help it to find out, what is the maximal income per day it can get.
Input
The first line of the input file contains n and m --- the number of cities in Europe we consider and the number of delivery routes respectively (2 ≤ n ≤ 100), 1 ≤ m ≤ 2000). The next line contains n - 1 integer numbers --- prices of a barrel of beer in European cities 2, 3 ..., n respectively (prices are positive integers and do not exceded 1000).
The following m lines contain four integer numbers each and describe delivery routes. Each route is specified by the numbers of cities it connects, its capacity, and the price of transporting one barrel of beer along it (the capacity and the price are positive integers, they do not exceed 1000).
There are multiple cases. Process to the end of file.
Output
Output the maximal income the company can get each day.
Sample Input
4 480 50 1301 2 80 502 4 40 903 1 40 603 4 30 50
Sample Output
3000
Hint
The company should deliver 80 barrels of beer to the second city (using the first route it costs 50 per barrel to deliver beer, income is 30 per barrel, 2400 total), and 30 barrels to the fourth city (the best path uses routes 3 and 4, it costs 110 to deliver a barrel, income is 20 per barrel, 600 total). It is not profitable to deliver beer to the third city, so the company should not do it.
Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #11
題意:有N個城市以及串連這些城市的M條無向邊,其中城市1是啤酒產地。給出N-1個數字,分別表示每個城市裡啤酒每桶的價格(城市1不算),我們可以認為這N-1個城市對啤酒的需求是沒有限制的即 無限大。
已知每條無向邊最多可以運送啤酒的桶數 和運送每桶的花銷,問你從城市1出發賣啤酒可以得到的最大收益。
思路:最大費用流。每次SPFA找最長路,找到後尋找最小可行流,然後增廣路徑。直到不存在源點到匯點的最長路或者最長路為負時停止。
建圖:設定一個超級源點source和超級匯點sink。
1,source到城市1建邊,容量為無窮,費用為0;
2,所有城市向超級匯點sink建邊,容量為無窮,費用為該城市每桶啤酒的價格。只要我們願意,可以選擇任意桶數的啤酒在這個城市賣出;
3,M條無向邊<u, v>,建雙向邊,容量為最多運送啤酒的桶數,費用為運送每桶花銷的相反數。
再次提醒下:最長路為負值時說明該路徑上的所有邊權之和為負值,更直白的說虧本了!這時就要停止增廣。
AC代碼:
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define MAXN 210#define MAXM 10000+10#define INF 0x3f3f3f3fusing namespace std;struct Edge{ int from, to, cap, flow, cost, next;};Edge edge[MAXM];int head[MAXN], edgenum;int pre[MAXN], dist[MAXN];bool vis[MAXN];int source, sink;//超級源點 超級匯點int N, M;//城市數 邊數void init(){ edgenum = 0; memset(head, -1, sizeof(head));}void addEdge(int u, int v, int w, int c){ Edge E1 = {u, v, w, 0, c, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, -c, head[v]}; edge[edgenum] = E2; head[v] = edgenum++;}void getMap(){ source = 0, sink = N+1; int a, b, c, d; addEdge(source, 1, INF, 0); for(int i = 2; i <= N; i++) { scanf("%d", &a); addEdge(i, sink, INF, a);//容量無窮大 因為每個城市的需求是沒有限制的 } while(M--) { scanf("%d%d%d%d", &a, &b, &c, &d); addEdge(a, b, c, -d);//建雙向邊 addEdge(b ,a, c, -d); }}bool SPFA(int s, int t){ queue<int> Q; memset(dist, -INF, sizeof(dist)); memset(vis, false, sizeof(vis)); memset(pre, -1, sizeof(pre)); dist[s] = 0; vis[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(dist[E.to] < dist[u] + E.cost && E.cap > E.flow)//尋找最長路 且 路徑上不能有滿流的邊 { dist[E.to] = dist[u] + E.cost; pre[E.to] = i; if(!vis[E.to]) { vis[E.to] = true; Q.push(E.to); } } } } return pre[t] != -1 && dist[t] >= 0;//最長路不能是負的 要不會賠錢}void MCMF(int s, int t, int &cost){ cost = 0; while(SPFA(s, t)) { int Min = INF; for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { Edge E = edge[i]; Min = min(Min, E.cap-E.flow); } for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { edge[i].flow += Min; edge[i^1].flow -= Min; cost += edge[i].cost * Min; } }}int main(){ while(scanf("%d%d", &N, &M) != EOF) { init(); getMap(); int cost; MCMF(source, sink, cost); printf("%d\n", cost); } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。