POJ 2516 Minimum Cost(最小费用最大流啊)

题目链接:?id=2516

Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.It’s known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places’ storage of K kinds of goods, N shopkeepers’ order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers’ orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places’ storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place.Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.The input is terminated with three "0"s. This test case should not be processed.

Output

For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".

Sample Input

1 3 3 1 1 10 1 11 2 21 0 11 2 31 1 12 1 11 1 132200 0 0

Sample Output

4-1

Source

, Wang Yijie

题意:

有N个店主,M个供应商,K种物品。

每个供应商对每种物品的的供应量已知,每个店主对每种物品的需求量的已知,从不同的供应商运送不同的货物到不同的店主手上需要不同的花费,又已知从供应商Mj送第kind种货物的单位数量到店主Ni手上所需的单位花费。

问:供应是否满足需求?如果满足,最小运费是多少?

转:

输入格式

在说解题思路之前,首先说说输入格式,因为本题的输入格式和解题时所构造的图的方向不一致,必须要提及注意。以样例1为例:

PS:

把 k 种物品分开来计算,每次对一种物品进行最小费用最大流计算。

对于每种物品,从源到供应商连接,,容量为供应商的储存量,费用为0。

采购商到汇连边,容量为需求量,费用为0。

供应商到采购商连边,容量为无穷,费用为对应的运费。

代码如下:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int MAXN = 117;const int MAXM = 100000;const int INF = 0x3f3f3f3f;struct Edge{int to,next,cap,flow,cost;} edge[MAXM];int head[MAXM],tol;int pre[MAXM],dis[MAXM];bool vis[MAXM];//int N;//节点总个数,节点编号从0~N-1int start, end;//源点和汇点void init(){tol = 0;memset(head,-1,sizeof(head));}void addedge(int u,int v,int cap,int cost)//左端点,右端点,容量,花费{edge[tol].to = v;edge[tol].cap = cap;edge[tol].cost = cost;edge[tol].flow = 0;edge[tol].next = head[u];head[u] = tol++;edge[tol].to = u;edge[tol].cap = 0;edge[tol].cost = -cost;edge[tol].flow = 0;edge[tol].next = head[v];head[v] = tol++;}bool spfa(int s,int t){queue<int> q;for(int i = 0; i < MAXM; i++){dis[i] = INF;vis[i] = false;pre[i] = -1;}dis[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){int v = edge[i].to;if(edge[i].cap > edge[i].flow &&dis[v] > dis[u] + edge[i].cost ){dis[v] = dis[u] + edge[i].cost;pre[v] = i;if(!vis[v]){vis[v] = true;q.push(v);}}}}if(pre[t] == -1){return false;}elsereturn true;}//返回的是最大流, cost存的是最小费用int minCostMaxflow(int s,int t,int &cost){int flow = 0;cost = 0;while(spfa(s,t)){int Min = INF;for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]){if(Min > edge[i].cap – edge[i].flow)Min = edge[i].cap – edge[i].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;}flow += Min;}return flow;}int sum = 0 ;int shop[MAXN][MAXN], supply[MAXN][MAXN];void build(int n, int m, int p){sum = 0;init();start = 0;end = n+m+1;for(int i = 0; i < m; i++)//源点到供应商{addedge(start, i+1, supply[i][p], 0);}for (int i = 0; i < n; i++)//店主到汇点{addedge(i+1+m, end, shop[i][p], 0);//左端点,右端点,容量,花费sum += shop[i][p];}for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){int cost;scanf("%d",&cost);//供应商到店主addedge(j+1, i+1+m, INF, cost);}}}int main(){int n, m, k;while(~scanf("%d%d%d",&n,&m, &k)){//n:店主 m:供应商 k:物品种类if(n==0 && m==0 && k==0){break;}int flag = 0;int ans = 0;for(int i = 0; i < n; i++){for(int j = 0; j < k; j++){scanf("%d",&shop[i][j]);//店主需求的数量}}for(int i = 0; i < m; i++){for(int j = 0; j < k; j++){scanf("%d",&supply[i][j]);//供应商能供应的数量}}for(int i = 0; i < k; i++){int flow, t_ans;build(n, m, i);if(!flag){flow = minCostMaxflow(start, end,t_ans);ans +=t_ans;}if(flow != sum)flag = 1;}if(flag){printf("-1\n");}else{printf("%d\n",ans);}}return 0;}

用爱生活,你会使自己幸福!用爱工作,你会使很多人幸福!

POJ 2516 Minimum Cost(最小费用最大流啊)

相关文章:

你感兴趣的文章:

标签云: