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

题意:n个商店,m个供货点,k种货物;如果建图一次建完太麻烦了,所以我们可以分k次进行建图,一次供货量小于需求的话,我们就能直接输出-1,第k张图都建一个超级源点和汇点,源点和供货点相连,流量为供货点的k货物的存货量,商店和汇点相连,流量为需要k货物的数量,供货点和商店之间相连,流量为inf,花费为输入的值。然后求k次最小花费,把k次最小费加起来就是答案

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<math.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2e5+10;
int S,T,cnt;
struct node
{
   int cap,v,to,cost;
}s[maxn];
int head[443],dis[443],vis[443],pre[443];
int nk[60][60],mk[60][60];//nk存商店对货物的需求,mk存供货点的货物数量
void add(int u,int v,int cap,int cost)
{
    s[cnt].v=v;
    s[cnt].cap=cap;
    s[cnt].cost=cost;
    s[cnt].to=head[u];
    head[u]=cnt++;
    s[cnt].v=u;
    s[cnt].cap=0;
    s[cnt].cost=-cost;
    s[cnt].to=head[v];
    head[v]=cnt++;
}
int spfa(int ss,int t)
{
    queue<int>q;
    q.push(ss);
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    memset(dis,inf,sizeof(dis));
    dis[ss]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int e=head[u];~e;e=s[e].to)
        {
            int v=s[e].v,cap=s[e].cap;
            if(cap&&dis[v]>dis[u]+s[e].cost)
            {
                dis[v]=dis[u]+s[e].cost;
                pre[v]=e;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
        vis[u]=0;
    }
    if(dis[t]!=inf)
        return 1;
    return 0;
}
int solve(int ss,int t,int kk)
{
    int flow=0,cost=0;
    while(spfa(ss,t))
    {
        int minf=inf;
        for(int i=pre[t];~i;i=pre[s[i^1].v])
        {
            if(s[i].cap<minf)
                minf=s[i].cap;
        }
        flow+=minf;
        for(int i=pre[t];~i;i=pre[s[i^1].v])
        {
            s[i].cap-=minf;
            s[i^1].cap+=minf;
            cost+=s[i].cost*minf;
        }

    }
    if(flow==nk[0][kk])
       return cost;
    return -1;
}
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        if(n==0&&m==0&&k==0)
            return 0;
        memset(nk,0,sizeof(nk));
        memset(mk,0,sizeof(mk));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=k;j++)
            {
                scanf("%d",&nk[i][j]);
                nk[0][j]+=nk[i][j];
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=k;j++)
            {
                scanf("%d",&mk[i][j]);
            }
        }
        int ans=0,flag=0;int x;
        for(int kk=1;kk<=k;kk++)
        {
            cnt=0;
            memset(head,-1,sizeof(head));
            for(int i=1;i<=m;i++)
                add(0,i,mk[i][kk],0);
            for(int i=1;i<=n;i++)
            {
                add(m+i,m+n+1,nk[i][kk],0);
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    scanf("%d",&x);
                    add(j,m+i,inf,x);
                }
            }
            if(flag!=-1)
            {
                flag=solve(0,n+m+1,kk);
                if(flag!=-1)
                    ans+=flag;
            }
        }
        if(flag==-1)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Kuguotao/article/details/83756664