POJ 2516 Minimum Cost 最大流的最小费用(不要拆点!)

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".

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".

题意:

M个卖家都有K种商品,每个卖家拥有每种商品不多于3个,N个买家都想要K种商品,每个买家每种商品想要的数量不超过3个,给出K张邻接矩阵,第k张邻接矩阵的 第i行第j列 表示把一个单位的 第k个物品 从卖家j那里送到买家i那里所需的路费。

求满足所有买家需求的最小路费,如果无法满足,输出-1.


解题思路:

一开始想的是拆点,嗯,把买家和卖家每一个商品的每一个单位拆分,T了,我tm在搞毛~~。

其实可以直接对k种商品都进行一次ford_fulkerson,然后累加每次的最小路费,详见代码。


代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <iomanip>
using namespace std;
int sumFlow;
const int MAXN = 50*50*3*2+10;
const int MAXM = (50*50*3)*(50*50*3)+2*50*50*3+100;
const int INF = 0x3f3f3f3f;
int need[60][60],has[60][60],cost[60][60][60];
int n,m,k;
struct Edge
{
    int u,v,cap,cost,nex;
}edge[MAXM];
int e_max;
int fir[MAXN], dist[MAXN], pre[MAXN];
bool vis[MAXN];
int q[9999999];
inline void init()
{
    e_max=0;
    memset(fir,-1,sizeof(fir));
}
inline void add_edge(int u,int v,int cap,int cost)
{
    edge[e_max].u=u;edge[e_max].v=v;edge[e_max].cap=cap;edge[e_max].cost=cost;
    edge[e_max].nex=fir[u];fir[u]=e_max++;
    edge[e_max].u=v;edge[e_max].v=u;edge[e_max].cap=0;edge[e_max].cost=-cost;
    edge[e_max].nex=fir[v];fir[v]=e_max++;
}
bool SPFA(int s,int t)
{
    int i,u,v;
    memset(vis,false,sizeof vis );
    memset(pre,-1,sizeof pre );
    memset(dist,INF,sizeof dist);
    vis[s]=true;
    dist[s]=0;
    q[0]=s;
    int f=0,r=1;
    while(f<r)
    {
        u=q[f++];
        vis[u]=false;
        for(i=fir[u];i!=-1;i=edge[i].nex)
        {
            v=edge[i].v;
            if(edge[i].cap&&dist[v]>dist[u]+edge[i].cost)
            {
                dist[v]=dist[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                        q[r++]=v;
                        vis[v]=true;
                }
            }
        }
    }
    if(dist[t]==INF)
        return false;
    return true;
}
int ford_fulkerson(int s,int t)
{
    int flow=0; // 总流量
    int i,minflow,mincost;
    mincost=0;
    while(SPFA(s,t))
    {
        minflow=INF+1;
        for(i=pre[t];i!=-1;i=pre[edge[i].u])
            if(edge[i].cap<minflow)
                minflow=edge[i].cap;
        flow+=minflow;
        for(i=pre[t];i!=-1;i=pre[edge[i].u])
        {
            edge[i].cap-=minflow;
            edge[i^1].cap+=minflow;
        }
        mincost+=minflow*dist[t];  //视情况而定。
    }
    sumFlow=flow; // 最大流
    return mincost;
}

void read()
{
        for(int i=1;i<=n;i++)
                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",&has[i][j]);
                }
        for(int w=1;w<=k;w++)
        {
                for(int i=1;i<=n;i++)
                        for(int j=1;j<=m;j++)
                        {
                                scanf("%d",&cost[w][i][j]);
                        }
        }
}
bool judge()
{
        for(int i=1;i<=k;i++)
        {
                int nn=0,mm=0;
                for(int j=1;j<=n;j++)
                        nn+=need[j][i];
                for(int j=1;j<=m;j++)
                        mm+=has[j][i];
                if(nn>mm)
                        return 0;
        }
        return 1;
}
int solve()
{
        int ans=0;
        for(int z=1;z<=k;z++)
        {
                init();
                for(int i=1;i<=n;i++)
                        add_edge(m+i,m+n+1,need[i][z],0);
                for(int i=1;i<=m;i++)
                        add_edge(0,i,has[i][z],0);
                for(int i=1;i<=n;i++)
                        for(int j=1;j<=m;j++)
                        add_edge(j,i+m,INF,cost[z][i][j]);
                ans+=ford_fulkerson(0,m+n+1);
        }
        return ans;
}
int main()
{
        while(scanf("%d%d%d",&n,&m,&k)&&(n+m+k))
        {
                read();
                if(!judge())
                {
                        printf("-1\n");
                        continue;
                }
                printf("%d\n",solve());
        }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值