【k次最小费用流】 POJ - 2516 E - Minimum Cost

E - Minimum Cost POJ - 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-th物品的需求量
m个卖家的库存,对于k-th物品的存储量
k个n*m的矩阵,每个卖家给每个买家的k-th物品的价格
求最小费用
走k次最小费用流

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int inf=0x3f3f3f3f;
typedef pair<int,int> P; //first保存最短距离,second保存顶点编号
struct edge
{
    int to,cap,cost,rev;
};
int V;//顶点数
const int MAXN=205;
const int maxn=55;
vector <edge> G[MAXN];//图的邻接表表示
int h[MAXN];//顶点的势
int dist[MAXN];//最短距离
int prevv[MAXN],preve[MAXN];//最短路中的前驱节点和对应的边
//向图中增加一条从from到to容量为cap费用为cost的边

void add_edge(int from,int to,int cap,int cost)
{
    G[from].push_back((edge){to,cap,cost,G[to].size()});
    G[to].push_back((edge){from,0,-cost,G[from].size()-1});
}

//求解从s到t流量为f的最小费用流
//如果没有流量为f的流,则返回-1
int min_cost_flow(int s,int t,int f)
{
    int res=0;
    memset(h,0,sizeof(h));  //初始化h
    while(f>0)
    {
        //使用dijstla算法更新h
        priority_queue< P,vector<P>,greater<P> > que;
        memset(dist,inf,sizeof(dist));
        dist[s]=0;
        que.push(P(0,s));
        while(!que.empty())
        {
            P p=que.top();que.pop();
            int v=p.second;
            if(dist[v]<p.first) continue;
            for(int i=0;i<G[v].size();i++)
            {
                edge &e=G[v][i];
                if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to])
                {
                    dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                    prevv[e.to]=v;
                    preve[e.to]=i;
                    que.push(P(dist[e.to],e.to));
                }
            }
        }
        if(dist[t]==inf)
        {
            //不能再增广
            return -1;
        }
        for(int v=0;v<V;v++) h[v]+=dist[v];
        //沿s到t的最短路尽量增广
        int d=f;
        for(int v=t;v!=s;v=prevv[v])
        {
            d=min(d,G[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*h[t];
        for(int v=t;v!=s;v=prevv[v])
        {
            edge &e=G[prevv[v]][preve[v]];
            e.cap-=d;
            G[v][e.rev].cap+=d;
        }
    }
    return res;
}

int place[maxn][maxn],money[maxn][maxn][maxn],order[maxn][maxn],sum_order[maxn];
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        V=MAXN;
        int res=0;
        int st=0,en=201;
        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]);
               // sum_order[j]+=order[i][j];
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=k;j++)
            {
                scanf("%d",&place[i][j]);
            }
        }
        for(int i=1;i<=k;i++)
        {
            for(int p=1;p<=n;p++)
            {
                for(int q=1;q<=m;q++)
                {
                    scanf("%d",&money[i][p][q]);
                }
            }
        }


        for(int K=1;K<=k;K++)
        {
            int sum=0;
            for(int i=0;i<=204;i++) G[i].clear();
            for(int i=1;i<=n;i++)
            {
                add_edge(st,i,order[i][K],0);
                sum+=order[i][K];
            }
            for(int i=1;i<=m;i++)
            {
                add_edge(100+i,en,place[i][K],0);
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    add_edge(i,100+j,inf,money[K][i][j]);
                }
            }
            int Q=min_cost_flow(st,en,sum);
            if(Q==-1)
            {
                res=-1;
                break;
            }
            else
                res+=Q;
        }
        printf("%d\n",res);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值