1. 程式人生 > >poj 2516費用流

poj 2516費用流

Minimum Cost

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 19116   Accepted: 6749

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

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

Source

POJ Monthly--2005.07.31

, Wang Yijie

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#include<vector>
#include<queue>
#define inf 1e9
#define maxn 205
using namespace std;
struct edge
{
    int from,to,cap,flow,cost;
    edge()
    {

    }
    edge(int f,int t,int ca,int fl,int co):from(f),to(t),cap(ca),flow(fl),cost(co){}
};
struct mcnf
{
    int n,m,s,t;
    vector<edge>edges;
    vector<int>g[maxn];
    bool inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
    void init(int n,int s,int t)
    {
        this->n=n;
        this->s=s;
        this->t=t;
        edges.clear();
        for(int i=0;i<n;i++)
        g[i].clear();
    }
    void addedge(int from,int to,int cap,int cost)
    {
        edges.push_back(edge(from,to,cap,0,cost));
        edges.push_back(edge(to,from,0,0,-cost));
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool bellford(int &flow,int &cost)
    {for(int i=0;i<n;i++)
    d[i]=inf;
    memset(inq,0,sizeof(inq));
    d[s]=0;
    a[s]=inf;
    inq[s]=true;
    p[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=false;
        for(int i=0;i<g[u].size();i++)
        {
            edge&e=edges[g[u][i]];
            if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
            {
                d[e.to]=d[u]+e.cost;
                p[e.to]=g[u][i];
                a[e.to]=min(a[u],e.cap-e.flow);
                if(!inq[e.to])
                {
                    q.push(e.to);
                    inq[e.to]=true;
                }
            }
        }

    }
    if(d[t]==inf)
    return false;
    flow+=a[t];
    cost+=a[t]*d[t];
    int u=t;
    while(u!=s)
    {
        edges[p[u]].flow+=a[t];
        edges[p[u]^1].flow-=a[t];
        u=edges[p[u]].from;

    }
    return true;

    }
    int mincost()
    {
        int flow=0,cost=0;
        while(bellford(flow,cost));
        return cost;

    }
}MM;
int n,m,k;
int need[50+5][50+5];
int have[50+5][50+5];
int cost[50+5][50+5][50+5];
int main()
{
while(~scanf("%d%d%d",&n,&m,&k)&&n)
{int goods[maxn];
int enough=true;
memset(goods,0,sizeof(goods));
for(int i=1;i<=n;i++)
    for(int j=1;j<=k;j++)
{scanf("%d",&need[i][j]);
    goods[j]+=need[i][j];
}
for(int i=1;i<=m;i++)
    for(int j=1;j<=k;j++)
{
    scanf("%d",&have[i][j]);
    goods[j]-=have[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[i][j][w]);
for(int i=1;i<=k;i++)
if(goods[i]>0)
{
    enough=false;
    break;
}
if(!enough)
{
    printf("-1\n");
    continue;
}
int minn=0;
for(int g=1;g<=k;g++)
{
 int src=0,dst=n+m+1;
 MM.init(n+m+2,src,dst);
 for(int i=1;i<=n;i++)
MM.addedge(src,i,need[i][g],0);
for(int i=1;i<=m;i++)
{
    MM.addedge(i+n,dst,have[i][g],0);
}
for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    MM.addedge(i,j+n,inf,cost[i][j][g]);
minn+=MM.mincost();

}
printf("%d\n",minn);
}
return 0;
}