POJ 2516 Minimum Cost k次最小费用流

Minimum Cost
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 17611 Accepted: 6191

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题意:题目讲的是要从M个货源地提供K种商品个N个商店,不同的商店对不同商品的需求量不同,不同货源地K中商品的存货量不同,不同商品从不同货源地到不同商店费用也不同,要求出满足所有商店要求的最小运输费用,若无法满足则输出“-1”;

说下输入:

首先n ,m ,k

然后是一个n*k的矩阵,(i,j)表示第i个商店对第k种商品的需求

然后是m * k 的矩阵,(i,j)表示第 i 个供货商 第 j 种货物 的存货

接下来是 k 个 n * m 的矩阵,第k个矩阵表示第i个商店从第j个货源地拿k商品的单位价格


分析:不满足的情况好办,对于任意一种商品,供货量小于需求量输出-1;

同样的,每个商品的最优解(局部最优解)也是全局最优解;

因此,对于每种商品都跑一次最小费用流,即可得到最优解;


AC CDOE:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 10000;
const int MAXE = MAXN*4;
const int INF = 0x3f3f3f3f;

struct node {
    int u,v,w;
    int cap,next;
} edge[MAXE] ;

int pre[MAXN];
int Head[MAXN];
int Dis[MAXN];
bool vis[MAXN];
int top;

void Init(){
    memset(Head,-1,sizeof(Head));
    top=0;
}

void AddEdge(int u,int v,int w,int cap) {
    edge[top].u=u;  edge[top].v=v;
    edge[top].w=w;  edge[top].cap=cap;
    edge[top].next=Head[u];
    Head[u] = top++;

    edge[top].u=v;  edge[top].v=u;
    edge[top].w=-w; edge[top].cap=0;
    edge[top].next=Head[v];
    Head[v]=top++;
}

int SPFA(int s,int t) {
    stack<int> Q;
    memset(Dis,INF,sizeof Dis);
    memset(vis,false,sizeof vis);
    memset(pre,-1,sizeof pre);
    Dis[s] = 0 ; vis[s]=true;
    Q.push(s);
    while(!Q.empty()) {
        int u = Q.top(); Q.pop();
        for(int i = Head[u]; i!=-1; i = edge[i].next) {
            int v = edge[i].v;
            int w = edge[i].w;
            if(edge[i].cap && Dis[v]>Dis[u]+w) {
                Dis[v] = Dis[u]+w;
                pre[v]=i;
                if(!vis[v]) {
                    vis[v]=true;
                    Q.push(v);
                }
            }
        }
        vis[u] = false;
    }
    if(Dis[t]==INF) return 0;
    else return 1;
}

int MCF(int s,int t){
    int MinFlow,MinCost=0,cnt=0;
    while(SPFA(s,t)){
        MinFlow=INF;
        //遍历最短路
        for(int k=pre[t];k!=-1;k=pre[edge[k].u]){
            MinFlow=min(MinFlow,edge[k].cap);
        }
        for(int k=pre[t];k!=-1;k=pre[edge[k].u]){
            edge[k].cap-=MinFlow;
            edge[k^1].cap+=MinFlow;
        }
        MinCost+=Dis[t]*MinFlow;
    }
    return MinCost;
}


int need[MAXN][MAXN];   //商品
int src[MAXN][MAXN];    //货源地
int p[MAXN][MAXN];      //价格
int srcsum[MAXN];

int main() {
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k)){
        if(n==0&&k==0&&m==0) break;
        int st=0,ed=n+m+1;
        memset(srcsum,0,sizeof srcsum);
        for(int i=1;i<=n;i++){
            //商店为1~n,货源为n+1 ~ n+m
            for(int j=1;j<=k;j++){
                scanf("%d",&need[i][j]);
            }
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=k;j++){
                scanf("%d",&src[i][j]);
                srcsum[j]+=src[i][j];
            }
        }

        int ans=0,flag=1;
        for(int k1=1;k1<=k;k1++){
            Init();
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    scanf("%d",&p[i][j]);
                    AddEdge(n+j,i,p[i][j],INF);
                }
            }
            
            for(int i=1;i<=m;i++){
                AddEdge(st,n+i,0,src[i][k1]);
            }
            int sum=0;
            for(int i=1;i<=n;i++){
                AddEdge(i,ed,0,need[i][k1]);
                sum+=need[i][k1];
            }
            if(sum>srcsum[k1]) flag=0;
            else ans+=MCF(st,ed);
        }
        if(flag) printf("%d\n",ans);
        else printf("-1\n");

    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值