poj 2516 Minimum Cost(最小费用流)

Language:
Minimum Cost
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 14165 Accepted: 4864

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种食品

数据:

N个店主对K种食品分别的订量

M个仓库对K种食品分别的存储量

K种食品的 从第j个仓库调往第i个店主所要的费用


K次构图 分别针对各种食品的 订量 存储量 所需的费用构图

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10000
#define MAXM 100000
#define INF 0x3f3f3f3f
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

void read(int &x)
{
    char ch;
    x=0;
    while(ch=getchar(),ch!=' '&&ch!='\n')
    {
        x=x*10+ch-'0';
    }
}

struct Edge
{
    int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n)
{
    N=n;
    tol=0;
    MEM(head,-1);
}

void addedge(int u,int v,int cap,int cost)
{
    edge[tol].to=v;
    edge[tol].cap=cap;
    edge[tol].cost=cost;
    edge[tol].flow=0;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].cost=-cost;
    edge[tol].flow=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}

bool spfa(int s,int t)
{
    queue<int> q;
    for(int i=0;i<N;i++)
    {
        dis[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1)  return false;
    return true;
}

int minCostMaxflow(int s,int t,int &cost)
{
    int flow=0;
    cost=0;
    while(spfa(s,t))
    {
        int Min=INF;
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
        {
            if(Min>edge[i].cap-edge[i].flow)
                Min=edge[i].cap-edge[i].flow;
        }
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
        {
            edge[i].flow+=Min;
            edge[i^1].flow-=Min;
            cost+=edge[i].cost*Min;
        }
        flow+=Min;
    }
    return flow;
}

int order[60][60],supply[60][60],mon[60][60][60];


int main()
{
//    fread;
    int N,M,K;
    while(scanf("%d%d%d",&N,&M,&K)!=EOF)
    {
        if(N==0&&M==0&&K==0)  break;
        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++)
            for(int j=1;j<=N;j++)
                for(int k=1;k<=M;k++)
                    scanf("%d",&mon[i][j][k]);
        int n=N+M;
        int s=0,t=n+1;
        int ans=0;
        for(int k=1;k<=K;k++)
        {
            init(t+1);
            int num=0;//记录从源出去的流量和
            for(int i=1;i<=N;i++)
            {
                addedge(s,i,order[i][k],0);
                num+=order[i][k];
            }
            for(int i=1;i<=N;i++)
            {
                for(int j=1;j<=M;j++)
                {
                    addedge(i,j+N,INF,mon[k][i][j]);
                }
            }
            for(int j=1;j<=M;j++)
            {
                addedge(j+N,t,supply[j][k],0);
            }
            int cost;
            int mi=minCostMaxflow(s,t,cost);
            int flag=0;
            if(mi!=num)
            {
                flag=1;
            }
            if(flag)//判可不可行
            {
                ans=-1;
                break;
            }
            ans+=cost;
        }
        printf("%d\n",ans);
    }
    return 0;
}


  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值