POJ2516-Minimum Cost 最小费用最大流

H - Minimum Cost
Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

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

用spfa求n次最大流

解决多源多汇网络问题,必须先构造与其等价的单源单汇网络。构造超级源s和超级汇t,定义各点编号如下:

 超级源s编号为0,供应商编号从1到M,店主编号从M+1到M+N,超级汇t编号为M+N+1。

令总结点数Nump=M+N+2,申请每条边的“花费”空间cost[Nump][ Nump]和“容量”空间cap[Nump][ Nump],并初始化为全0。

超级源s向所有供应商M建边,费用为0,容量为供应商j的供应量。

       每个供应商都向每个店主建边,正向弧费用为输入数据的第kind个矩阵(注意方向不同),容量为供应商j的供应量;反向弧费用为正向弧费用的负数,容量为0。

所有店主向超级汇t建边,费用为0,容量为店主i的需求量。

 

注意:1、其他没有提及的边,费用和容量均为0,容量为0表示饱和边或不连通。

   2、计算每种物品的最小费用都要重复上述工作重新构图,不过存储空间cost和cap不必释放,可重新赋值再次利用。

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 305
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
struct Edge{
    int u,w,v,len,next;
}e[100*maxn];
int head[maxn],cnt,s,t;
int pre[maxn],mc[maxn];
int need[maxn][maxn],offer[maxn][maxn],carriage[maxn][maxn][maxn];
int spfa(int s,int t){
    int u,v;
    int dist[maxn],visit[maxn];
    queue<int>q;
    MT(dist,0x3f);
    MT(visit,0);
    MT(pre,-1);
    MT(mc,0x3f);
    dist[s]=0;
    visit[s]=1;
    q.push(s);
    while(!q.empty()){
        u=q.front();
        q.pop();
        visit[u]=0;
        for(int k=head[u];k;k=e[k].next){
            v=e[k].v;
            if(e[k].len&&dist[v]>dist[u]+e[k].w){
                dist[v]=dist[u]+e[k].w;
                pre[v]=k;
                mc[v]=min(mc[u],e[k].len);
                if(!visit[v]){
                    q.push(v);
                    visit[v]=1;
                }
            }
        }
    }
    if(dist[t]<INF)return dist[t];
    return -1;
}
void handle(int flow){
    for(int i=t;pre[i]+1;i=e[pre[i]].u){
        e[pre[i]].len-=flow;
        e[pre[i]^1].len+=flow;
    }
}
void add(int u,int v,int w,int len){
    cnt++;
    e[cnt].u=u;e[cnt].v=v;e[cnt].w=w;e[cnt].len=len;
    e[cnt].next=head[u];head[u]=cnt;
}
void bulid(int n,int m,int p){
    MT(head,0);
    cnt=1;s=0;t=n+m+1;
    FOR(i,1,m){
        add(s,i,0,offer[p][i]);
        add(i,s,0,0);
    }
    FOR(i,1,n){
        add(i+m,t,0,need[p][i]);
        add(t,i+m,0,0);
    }
    FOR(i,1,m){
        FOR(j,1,n){
            add(i,j+m,carriage[p][i][j],INF);
            add(j+m,i,-carriage[p][i][j],0);
        }
    }
}
int main(){
    int ans,sum;
    int n,m,p;
    int sum_of_offer[maxn],sum_of_need[maxn],flag;
    while(cin>>n>>m>>p&&(n+m+p)){
        ans=0;flag=1;
        MT(sum_of_need,0);
        MT(sum_of_offer,0);
        FOR(i,1,n)FOR(j,1,p){rd(need[j][i]);sum_of_need[j]+=need[j][i];}
        FOR(i,1,m)FOR(j,1,p){rd(offer[j][i]);sum_of_offer[j]+=offer[j][i];}
        FOR(i,1,p)FOR(j,1,n)FOR(k,1,m)rd(carriage[i][k][j]);
        FOR(i,1,p)
            if(sum_of_offer[i]<sum_of_need[i]){
                flag=0;
                break;
            }
        if(!flag){
            printf("-1\n");
            continue;
        }
        FOR(i,1,p){
            bulid(n,m,i);
            sum=0;
            int k;
            while(k=spfa(s,t),k+1){
                sum+=mc[t]*k;
                handle(mc[t]);
            }
            ans+=sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
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
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值