poj2516——Minimum Cost(最小费用最大流)

来源:互联网 发布:网络测试工具包 编辑:程序博客网 时间:2024/05/16 00:48

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

给出商店数n,供应点数m和物品种类数k
再给出每个商店需要的每个物品的个数
再给出每个供应点能供应的每个物品的个数
再给出k个矩阵表示从供应点到商店需要的费用,行表示商店,列表示供应点。

接下来就是建图,源点0到供应点1~m,无费用,容量是供应点能供应的食品数量
供应点1~m到商店m+1~m+n,费用是题目的输入,容量无限
商店m+1~m+n到汇点m+n+1,无费用,容量是商店需要的食物数量
然后对每种食物求最小费用最大流,最后把最小费用加起来
注意矩阵是n*m,费用却是从m到n,注意其中的转换

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <map>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 1005#define Mod 10001using namespace std;int k;int n,m,source,ending,mincost,maxflow;int res[MAXN][MAXN],cost[MAXN][MAXN],parent[MAXN],d[MAXN];int need[MAXN],have[MAXN],nn[MAXN][MAXN],hh[MAXN][MAXN];void spfa(){    queue<int> q;    bool vis[MAXN];    memset(vis,false,sizeof(vis));    memset(parent,-1,sizeof(parent));    for(int i=0; i<=ending; ++i)        d[i]=INF;    q.push(0);    d[0]=0;    vis[0]=true;    while(!q.empty())    {        int v=q.front();        q.pop();        vis[v]=false;        for(int i=0; i<=ending; ++i)        {            if(res[v][i]&&d[v]+cost[v][i]<d[i])            {                d[i]=d[v]+cost[v][i];                parent[i]=v;                if(!vis[i])                {                    q.push(i);                    vis[i]=true;                }            }        }    }}void MCMF(){    int v,minflow;    maxflow=0;    while(1)    {        spfa();        if(parent[ending]==-1)            break;        minflow=INF;        v=ending;        while(parent[v]!=-1)        {            minflow=min(minflow,res[parent[v]][v]);            v=parent[v];        }        v=ending;        while(parent[v]!=-1)        {            res[parent[v]][v]-=minflow;            res[v][parent[v]]+=minflow;            v=parent[v];        }        maxflow+=minflow;        mincost+=d[ending]*minflow;//最后求的是总费用=单价*数量    }}int main(){    while(~scanf("%d%d%d",&n,&m,&k))    {        if(n==0&&m==0&&k==0)            break;        source=0,ending=n+m+1,mincost=0;        bool flag=false;        memset(need,0,sizeof(need));        memset(have,0,sizeof(have));        for(int i=1; i<=n; ++i)            for(int j=1; j<=k; ++j)            {                scanf("%d",&nn[i][j]);                need[j]+=nn[i][j];            }        for(int i=1; i<=m; ++i)            for(int j=1; j<=k; ++j)            {                scanf("%d",&hh[i][j]);                have[j]+=hh[i][j];            }        for(int i=1; i<=k; ++i)            if(have[i]<need[i])                flag=true;        for(int r=1; r<=k; ++r)        {            memset(cost,0,sizeof(cost));            for(int i=1; i<=n; ++i)                for(int j=1; j<=m; ++j)                {                    scanf("%d",&cost[j][i+m]);                    cost[i+m][j]=-cost[j][i+m];                }            if(flag)                continue;            memset(res,0,sizeof(res));            for(int i=1; i<=n; ++i)                res[i+m][ending]=nn[i][r];            for(int i=1; i<=m; ++i)                res[source][i]=hh[i][r];            for(int i=1; i<=n; ++i)                for(int j=1; j<=m; ++j)                    res[j][i+m]=INF;            MCMF();        }        if(flag)            printf("-1\n");        else            printf("%d\n",mincost);    }    return 0;}
0 0
原创粉丝点击