POJ2516——分治+最小费用最大流

本文深入探讨了最小费用最大流算法在解决供应商与商店间商品配送问题的应用。通过实例讲解,介绍了如何构建网络流图,包括源点、汇点、供应商节点、商店节点及它们之间的连接方式和费用计算。特别关注了如何处理供不应求的情况,以及如何通过多次运行算法确保总费用最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=2516

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种商品
接下来 n*k的矩阵,表示每个商店需要每个商品的数目;
再接下来m*k矩阵,表示每个提供商拥有每个商品的个数。
然后,对于每个物品k,都有n*m的矩阵。
i行j列表示:
从j提供商向i商店运送一个k商品的代价是多少。
判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用。(摘抄自网上)

 

比较经典的最小费用最大流,而且要分治

首先我们需要对每一个商品(相互独立)进行处理,求出最小费用,最后求和,还能保证费用仍然是最小的。

PS:如果对所有商品同时进行处理,好像是爆时间,而且还麻烦!!(QAQ)

然后就是对每个商品怎么建图了,其实也很简单!

总的思路是:s(n+m)->供应商->商店->t(n+m+1)

源点s到供应商的费用是0,容量是供应量。

供应商到商店的费用是该商品运输费费用,容量为0。

商店到汇点t的费用为0,容量为需求量。

当然还有为-1的情况,就是供不应求了,不过分为两种。

一种是某个商品最初提供时就发现供不应求了。

一种是在某个商品跑完最大流后发现最大流仍然小于需求量。

这里用的最小费用最大流的求法是来自刘汝佳紫书里的模板。

上代码!!!!!!!!!!!

 

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue> 
using namespace std;
const int maxn=1024;
const int INF=0x3f3f3f3f;
struct Edge{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};
struct MCMF{
    int n,m;
    vector<Edge>edges;
    vector<int>G[maxn];
    int inq[maxn];//是否在队列中
    int d[maxn];//Bellman-Ford
    int p[maxn];//上一条弧
    int a[maxn];//可改进量

    void init(int n){
        this->n=n;
        for(int i=0;i<n;i++)G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int cap,int cost){
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BellmanFord(int s,int t,int &flow,long long &cost){
        for(int i=0;i<n;i++)d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0;inq[s]=1;p[s]=0;a[s]=INF;

        queue<int>Q;
        Q.push(s);
        while(!Q.empty()){
            int u=Q.front();Q.pop();
            inq[u]=0;
            for(int i=0;i<(int)G[u].size();i++){
                Edge &e=edges[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){Q.push(e.to);inq[e.to]=1;}
                }
            }
        }
        if(d[t]==INF)return false;
        flow+=a[t];
        cost+=(long long)d[t]*(long long)a[t];
        for(int u=t;u!=s;u=edges[p[u]].from){
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
        }
        return true;
    }

    //需要保证初始网络中没有负权圈
    int MincostMaxflow(int s,int t,long long &cost){
        int flow=0;cost=0;
        while(BellmanFord(s,t,flow,cost));
        return flow;
    }
}MM;
int main(int argc, char** argv) {
	long long mincost=0;
	int maxflow;
	int n,m,k;
	pair<int,int> a[100];
	while(~scanf("%d%d%d",&n,&m,&k)&&n&&m&&k){
		memset(a,0,sizeof(a));
		int flag=1;
		long long totalcost=0;
		int totalflow=0;
		int storage[100][100],need[100][100],cost[100][100];
		memset(storage,0,sizeof(storage));
		memset(need,0,sizeof(need));
		for(int i = 0;i<n;++i){
			for(int j = 0;j<k;++j){
				scanf("%d",&need[i][j]);
				a[j].first+=need[i][j];//统计第 j 个商品总需求量 
			}
		}
		for(int i = 0;i<m;++i){
			for(int j = 0;j<k;++j){
				scanf("%d",&storage[i][j]);
				a[j].second+=storage[i][j];//统计第 j 个商品总供应量 
			}
		}
		for(int i = 0;i<k;++i){
			if(a[i].first>a[i].second){//如果第 i 个商品供不应求 
				flag=0;
				break;
			}
		}
		
		for(int t=0;t<k;++t){//对每个商品求一次最小费用最大流 
			MM.init(n+m+2);//初始化放在里面,因为要跑 k 次最小费用最大流 
			for(int i = 0;i<n;++i){
				for(int j = 0;j<m;++j){
					scanf("%d",&cost[i][j]);//从第 j 个供应商到第 i 个商店运输第 k 个商品所用的运输花销
					MM.AddEdge(j,m+i,INF,cost[i][j]);//供应商和商店建边,费用为运输成本,容量为无穷大。因为这里运输的量是无穷多的 
				}
			}
			if(!flag) continue;
			int S=n+m;
			int T=n+m+1;//初始化汇点和源点 
			for(int i=0;i<m;++i)
				MM.AddEdge(S,i,storage[i][t],0);//源点和每个商店建边,费用为0,容量为供应量 
			for(int i = 0;i<n;++i)
				MM.AddEdge(m+i,T,need[i][t],0);//汇点和每个供应商建边,费用为0, 容量为需求量 
			maxflow=MM.MincostMaxflow(S,T,mincost);
			if(maxflow<a[t].first) flag=0;//如果该商品的最大供应量还是要小于需求量 
			
			totalcost+=mincost;//统计每次的最小花费。 
//			totalflow+=maxflow;
		}
		if(!flag) printf("-1\n");//供不应求 
		else printf("%lld\n",totalcost);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值