POJ 2516 Minimum Cost (最小费用最大流)
Posted 季末Despaire
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2516 Minimum Cost (最小费用最大流)相关的知识,希望对你有一定的参考价值。
Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
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.
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.
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 1 0 1 1 1 2 2 1 0 1 1 2 3 1 1 1 2 1 1 1 1 1 3 2 20 0 0 0
Sample Output
4
-1
题意:有k种物品,m个供应商,n个收购商。每个供应商和收购商都需要一些种类的物品若干。每个供应商与每个收购商之间的对于不同物品的运费是不同的。求满足收购商要求的情况下的最小运费。
分析:最小费用最大流,最大流的前提下求最小费用。这题我们可以把k种物品分开计算,每次对一种物品进行最小费用最大流计算。如果不分开算会超时。对于每种物品,从源到供应商连接,容量为供应商的储存量,费用为0。采购商到汇连边,容量为需求量,费用为0。供应商到采购商连边,容量为无穷,费用为对应的运费。
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; //************************************************************ //最小费用最大流算法 //SPFA求最短路 //邻接矩阵形式 //初始化:cap:容量,没有边为0 //cost:耗费,对称形式,没有边的也为0 //c是最小费用 //f是最大流 //******************************************************* const int MAXN=500; const int INF=0x3fffffff; int cap[MAXN][MAXN];//容量,没有边为0 int flow[MAXN][MAXN]; //耗费矩阵是对称的,有i到j的费用,则j到i的费用为其相反数 int cost[MAXN][MAXN]; int n;//顶点数目0~n-1 int f;//最大流 int c;//最小费用 int st,ed;//源点和汇点 bool vis[MAXN];//在队列标志 int que[MAXN]; int pre[MAXN]; int dis[MAXN];//s-t路径最小耗费 bool SPFA() { int front=0,rear=0; for(int u=0;u<=n;u++) { if(u==st) { que[rear++]=u; dis[u]=0; vis[u]=true; } else { dis[u]=INF; vis[u]=false; } } while(front!=rear) { int u=que[front++]; vis[u]=false; if(front>=MAXN)front=0; for(int v=0;v<=n;v++) { if(cap[u][v]>flow[u][v]&&dis[v]>dis[u]+cost[u][v]) { dis[v]=dis[u]+cost[u][v]; pre[v]=u; if(!vis[v]) { vis[v]=true; que[rear++]=v; if(rear>=MAXN)rear=0; } } } } if(dis[ed]>=INF)return false; return true; } void minCostMaxflow() { memset(flow,0,sizeof(flow)); c=f=0; while(SPFA()) { int Min=INF; for(int u=ed;u!=st;u=pre[u]) Min=min(Min,cap[pre[u]][u]-flow[pre[u]][u]); for(int u=ed;u!=st;u=pre[u]) { flow[pre[u]][u]+=Min; flow[u][pre[u]]-=Min; } c+=dis[ed]*Min; f+=Min; } } //************************************************************ int order[MAXN][MAXN]; int supply[MAXN][MAXN]; int total[MAXN]; int main() { int N,M,K; while(scanf("%d%d%d",&N,&M,&K)==3){ if(N==0&&M==0&&K==0) break; st=0,ed=n=N+M+1; memset(cap,0,sizeof(cap)); memset(cost,0,sizeof(cost)); for(int i=1;i<=N;i++) for(int j=1;j<=K;j++) scanf("%d",&order[i][j]); for(int i=1;i<=M;i++) for(int j=1;j<=K;j++) scanf("%d",&supply[i][j]); for(int i=1;i<=K;i++){ total[i]=0; for(int j=1;j<=N;j++) total[i]+=order[j][i]; } for(int i=1;i<=M;i++) for(int j=M+1;j<=M+N;j++) cap[i][j]=INF; int ans=0; bool flag=true; for(int i=1;i<=K;i++){ for(int j=M+1;j<=M+N;j++){ for(int k=1;k<=M;k++){ scanf("%d",&cost[k][j]); cost[j][k]=-cost[k][j]; } } if(!flag) continue; for(int j=1;j<=M;j++) cap[st][j]=supply[j][i]; for(int j=1;j<=N;j++) cap[j+M][ed]=order[j][i]; minCostMaxflow(); if(f<total[i]) flag=false; else ans+=c; } if(!flag) printf("-1\n"); else printf("%d\n",ans); } return 0; }
以上是关于POJ 2516 Minimum Cost (最小费用最大流)的主要内容,如果未能解决你的问题,请参考以下文章
Minimum Cost POJ - 2516 (模板题 spfa最小费用最大流)