poj 2516 Minimum(SPFA解决最小费用最大流)

来源:互联网 发布:数据分析培训 编辑:程序博客网 时间:2024/06/05 00:54
Minimum Cost
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 16007 Accepted: 5614

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 10 1 11 2 21 0 11 2 31 1 12 1 11 1 132200 0 0

Sample Output

4-1

Source

POJ Monthly--2005.07.31, Wang Yijie

提示

题意:

一个货物供应公司,需要把货物供给n(0<n<50)个销售商,该公司有m(0<m<50)个仓库,并且提供k(0<k<50)种货物,每个仓库和每种货物运输费是不一样的,请求出所需要的最小花费,如果不能达到供应要求则输出"-1"。

思路:

SPFA解决最小费用最大流,数据范围较小就用邻接矩阵吧。

样例输入是这样的:

先给出n,m,k,之后是n行数据,对应第几个店家所需每种货物的数量。接下来的m行,对应每个仓库每种货物的库存量。最后有k个n*m的矩阵,表示第几种货物从第mi个仓库送到第ni个店家单位数量货物所花费的运输费。这个矩阵如果不把它变成m*n会不容易与前面给出的数据对应起来。

对每种物品进行运算,求和就可以了。

示例程序

Source CodeProblem: 2516Code Length: 3192BMemory: 484KTime: 250MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>#define MAX 1000000007int map[100][100],cost[100][100],offer[50][50],need[50][50],sumoffer[50],sumneed[50],pre[100];int spfa(int s,int t){    int pos,i,d[100],v[100],q[10000],f=0,top=0;    for(i=0;t>=i;i++)    {        d[i]=MAX;        v[i]=0;    }    v[s]=1;    d[s]=0;    q[top]=s;    top++;    while(f<top)    {        pos=q[f];        v[pos]=0;        for(i=0;t>=i;i++)        {            if(map[pos][i]!=0&&d[i]>d[pos]+cost[pos][i])            {                d[i]=d[pos]+cost[pos][i];                pre[i]=pos;//这里与普通的spfa有所不同,这是必须的                if(v[i]==0)                {                    v[i]=1;                    q[top]=i;                    top++;                }            }        }        f++;    }    if(d[t]!=MAX)    {        return 1;    }    else    {        return 0;    }}int maxflow(int s,int t){    int i,min,sum=0;    min=MAX;    while(spfa(s,t)==1)    {        for(i=t;i!=s;i=pre[i])        {            if(min>map[pre[i]][i])            {                min=map[pre[i]][i];            }        }        for(i=t;i!=s;i=pre[i])        {            map[pre[i]][i]=map[pre[i]][i]-min;            map[i][pre[i]]=map[i][pre[i]]+min;            sum=sum+cost[pre[i]][i]*min;        }    }    return sum;}int main(){    int n,m,k,i,i1,i2,s,t,sum,bug;    scanf("%d %d %d",&n,&m,&k);    while(n!=0||m!=0||k!=0)    {        memset(sumoffer,0,sizeof(sumoffer));        memset(sumneed,0,sizeof(sumneed));        sum=0;        bug=0;        for(i=1;n>=i;i++)//需求量输入        {            for(i1=1;k>=i1;i1++)            {                scanf("%d",&need[i][i1]);                sumneed[i1]=sumneed[i1]+need[i][i1];            }        }        for(i=1;m>=i;i++)//供应量输入        {            for(i1=1;k>=i1;i1++)            {                scanf("%d",&offer[i][i1]);                sumoffer[i1]=sumoffer[i1]+offer[i][i1];            }        }        for(i=1;k>=i;i++)//判断能否完成运输任务        {            if(sumneed[i]>sumoffer[i])            {                bug=1;                break;            }        }        s=0;//源点        t=n+m+1;//终点        for(i=1;k>=i;i++)//把每种物品拆开计算求和就行了        {            memset(cost,0,sizeof(cost));            memset(map,0,sizeof(map));            for(i1=1;n>=i1;i1++)            {                for(i2=1;m>=i2;i2++)                {                    scanf("%d",&cost[i2][i1+m]);                    cost[i1+m][i2]=-cost[i2][i1+m];//还需要建反向边                }            }            if(bug==1)//不能完成任务就不需要执行            {                continue;            }            for(i1=1;m>=i1;i1++)//建立图            {                map[s][i1]=offer[i1][i];            }            for(i1=1;m>=i1;i1++)            {                for(i2=1;n>=i2;i2++)                {                    map[i1][i2+m]=offer[i1][i];                }            }            for(i1=1;n>=i1;i1++)            {                map[i1+m][t]=need[i1][i];            }            sum=sum+maxflow(s,t);        }        if(bug==1)        {            printf("-1\n");        }        else        {            printf("%d\n",sum);        }        scanf("%d %d %d",&n,&m,&k);    }    return 0;}

0 0
原创粉丝点击