Minimum Cost ——多源点最小费用最大流

E - Minimum Cost

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

题意:

        Dearboy 要提供货物给N个店主。Dearboy 的旗下有M个供应点,这M个供应点都将提供K件不同的商品;

输入:N、M、K;

接着输入N行,表示Ni店所需的不同K的个数——nk[MM][MM];

再接着输入M行,表示Mi所有的不同的K的个数——mk[MM][MM];

然后输入K个矩阵,每个矩阵为N*M。表示Ki商品从Mi运输到Ni所需的花费——x;

结果求——满足所有店主的最小花费,如果不满足,则输出-1;

思路:

        因为输入有K个矩阵,可以把这K个 分开求 最大流;

建立一个源点,把K当作汇点:

  • 把源点和每一个供货点相连,流量为 供货点供应Ki货物 的存量,花费为0
  • 把店主和当前枚举的Ki货物相连,流量为当前店主需要的Ki货物量,花费为0
  • 然后再给每个供货点和店主建边,流量为 供货点供应Ki货物 的存量,花费为运送当前货物的花费

代码:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MM=5010;
const int NN=50010;
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
int maxflow,len;
int first[MM];
int dis[MM],pre[MM];
bool vis[MM];
int st,en,n,m,k;

struct Edge
{
    int v,next,cap;
    int cost,flow;
} E[NN*2];
void init()
{
    mem(first,-1);
    len=0;
    maxflow=0;
    st=0;
    en=n+m+1;
}
void add_edge(int u,int v,int c,int cost)
{
    E[len].v=v;
    E[len].cap=c;
    E[len].flow=0;
    E[len].cost=cost;
    E[len].next=first[u];
    first[u]=len++;
}
void add(int u,int v,int c,int cost)
{
    add_edge(u,v,c,cost);
    add_edge(v,u,0,-cost);
}
bool spfa(int s,int t)
{
    int i,kk,v;
   queue<int>qu;
    mem(vis,0);
    mem(pre,-1);
    mem(dis,INF);

    vis[s]=1;
    dis[s]=0;
    qu.push(s);

    while(!qu.empty())
    {
        kk=qu.front();
        qu.pop();
        vis[kk]=0;
        for(i=first[kk]; i!=-1; i=E[i].next)
        {
            v=E[i].v;
            if(E[i].cap>E[i].flow&&dis[v]>dis[kk]+E[i].cost)
            {
                dis[v]=dis[kk]+E[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    qu.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    if(dis[t]==INF)
        return false;
    return true;
}

int MCMF(int s,int t)
{
    int d,mincost=0;
    while(spfa(s,t))
    {
        d=INF;
        for(int i=pre[t]; i!=-1; i=pre[E[i^1].v])
            d=min(d,E[i].cap-E[i].flow);
        maxflow += d;
        for(int i=pre[t]; i!=-1; i=pre[E[i^1].v])
        {
            E[i].flow+=d;
            E[i^1].flow-=d;
        }
        mincost += dis[t]*d;
    }
    return mincost;
}
int nk[MM][MM],mk[MM][MM],nn[MM]; //nn[]存这几家店所需的商品Ki数量和。
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k)&&(n||m||k))
    {
        int x,i,j,v;
        int mincost=0,ans=0,flag=1;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=k; j++)
            {
                scanf("%d",&x);
                nk[i][j]=x;
                nn[j]+=x;
            }
        }
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=k; j++)
            {
                scanf("%d",&x);
                mk[i][j]=x;
            }
        }
        for(i=1; i<=k; i++)
        {
            init();
            for(j=1; j<=n; j++)
            {
                for(v=1; v<=m; v++)
                {
                    scanf("%d",&x);
                    add(st+v,st+m+j,mk[v][i],x);
                }
            }
            if(flag==0)   //flag=0,表示Dearboy不能满足店主的需求
                continue;   //因为还要输入
            for(j=1;j<=n;j++)
                add(st+m+j,en,nk[j][i],0);
            for(v=1;v<=m;v++)
                add(st,st+v,mk[v][i],0);
            mincost=MCMF(st,en);
            if(maxflow!=nn[i])   //如果最大流不满足所需该商品的数量
            {
                flag=0;
                continue;
            }
            else
            {
                ans+=mincost;
            }
        }
        if(flag)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/team39/article/details/81708346