1. 程式人生 > >Minimum Cost ——多源點最小費用最大流

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;
}