POJ - 2516 Minimum Cost(最小花费最大流)


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 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 
题意:
有N个店主,M个供应商,K种物品。每个供应商对每种物品的的供应量和每个店主对每种物品的需求量的已知。不同的供应商运送不同的货物到不同的店主需要不同的花费。已知从供应商j送第k种货物的单位数量到店主i手上所需的单位花费。
问:供应是否满足需求?如果满足,输出最小花费。如果不满足,输出-1。
输出解析:

题解:
因为涉及N,M,K三种元素,而我们平常做的都是涉及两种元素的题,所以我们很容易想到取出其中的两类元素来当成普通的
最小花费最大流题做,再将多个结果相加。
做这道题时根据输入数据顺序以及关系自然而然的就选了N,M(我也没想原因,直接就选了,跟着感觉走。)
然后就简单了,添加超级源点超级汇点,分别建立超源到M的边(费用为0,容量无限),M到N的边(费用为题给的花费,容量为供应商供应量),N到超汇的边(费用为0,容量为店主需求量)。然后就按基本的套路走就行了。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 210;

struct Edge{
	int value,flow,to,rev;
	Edge(){}
	Edge(int a,int b,int c,int d):to(a),value(b),flow(c),rev(d){}
};

vector<Edge> E[MAXN];

inline void Add(int from,int to,int value,int flow){
	
	E[from].push_back(Edge(to,value,flow,E[to].size()));
	E[to].push_back(Edge(from,-value,0,E[from].size()-1));
	
}

bool book[MAXN];//用于SPFA中标记是否在queue中 
int cost[MAXN];//存费用的最短路径 
int pre[MAXN];//存前节点 
int pree[MAXN];//存在前节点的vector中的下标 

bool Spfa(int from,int to){
	
	memset(book,false,sizeof book);
	memset(cost,INF,sizeof cost);
	
	book[from] = true;
	cost[from] = 0;
	
	queue<int> Q;
	Q.push(from);
	
	while(!Q.empty()){
		
		int t = Q.front();
		Q.pop();
		book[t] = false;
		
		for(int i=0 ; i<E[t].size() ; ++i){
			
			Edge& e = E[t][i];
			
			if(e.flow > 0 && cost[e.to] > cost[t] + e.value){
				
				cost[e.to] = cost[t] + e.value;
				pre[e.to] = t;
				pree[e.to] = i;
			
				if(book[e.to] == false){
					Q.push(e.to);
					book[e.to] = true;
				}
				
			}
			
		}	
		
	}
	return cost[to] != INF;
}

int Work(int from,int to){
	
	int sum = 0;
	
	while(Spfa(from,to)){
		
		int mflow = INF;
		int flag = to;
		
		while(flag != from){
			mflow = min(mflow,E[pre[flag]][pree[flag]].flow);
			flag = pre[flag];
		}
		
		flag = to;
		
		while(flag != from){
			sum += E[pre[flag]][pree[flag]].value * mflow;
			
			E[pre[flag]][pree[flag]].flow -= mflow;
			E[flag][E[pre[flag]][pree[flag]].rev].flow += mflow;
			
			flag = pre[flag];
		}
		
	}
	
	return sum;
	
}

int main(){
	
	int N,M,K;
	int board[55];//用于验证供求关系。 
	int need[55][55];//N*K
	int have[55][55];//M*K
	int price[55][55][55];//K*N*M;
	
	while(scanf("%d %d %d",&N,&M,&K) && (N || M || K)){
		memset(board,0,sizeof board);
		
		//-------------------------------------
		//这一块负责读入数据 
		for(int i=1 ; i<=N ; ++i){
			for(int j=1 ; j<=K ; ++j){
				scanf("%d",&need[i][j]);
				board[j] -= need[i][j];
			}
		}
		for(int i=1 ; i<=M ; ++i){
			for(int j=1 ; j<=K ; ++j){
				scanf("%d",&have[i][j]);
				board[j] += have[i][j];
			}
		}
		for(int i=1 ; i<=K ; ++i){
			for(int j=1 ; j<=N ; ++j){
				for(int k=1 ; k<=M ; ++k){
					scanf("%d",&price[i][j][k]);
				}
			}
		}
		//----------------------------------------- 
		
		//-----------------------------------------
		//这一块负责验证是否供小于求 
		bool flag = false;
		for(int i=1 ; i<=K ; ++i){
			if(board[i] < 0){
				printf("-1\n");
				flag = true;
				break;
			}
		}
		if(flag)continue;
		//-----------------------------------------
		 
		//-----------------------------------------
		//这一块负责求值 
		int sum = 0;
		for(int i=1 ; i<=K ; ++i){
			
			for(int j=0 ; j<MAXN ; ++j)E[j].clear();
			for(int j=1 ; j<=N ; ++j)Add(j+50,MAXN-1,0,need[j][i]);//建立 N到超汇的边 
			for(int j=1 ; j<=M ; ++j)Add(0,j,0,have[j][i]);//建立超源到 M的边
			for(int j=1 ; j<=N ; ++j){//建立 M到N的边 
				for(int k=1 ; k<=M ; ++k){
					Add(k,j+50,price[i][j][k],INF);
				}
			}
			sum += Work(0,MAXN-1);
		}
		//------------------------------------------
		 
		printf("%d\n",sum);
	}
	
	return 0;
} 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值