程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 2516 Minimum Cost

POJ 2516 Minimum Cost

編輯:C++入門知識

Minimum Cost
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 11779   Accepted: 3971

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
   最小費用最大流,建好圖,利用改進的EK() 就可以了
[cpp]
#include <stdio.h>  
#include <string.h>  
#include <math.h>  
int a[60][60],b[60][60],c[60][60][60]; 
int cost[100][200],cap[100][200],dis[200]; 
int queue[1000000],pre[200],status[200]; 
int INF=0x7fffffff,sta,end; 
int main() 

    int EK(); 
    int i,j,n,m,s,t,k,u,s1,s2; 
    while(scanf("%d %d %d",&n,&m,&k)!=EOF) 
    { 
        if(n==0&&m==0&&k==0) 
        { 
            break; 
        } 
        for(i=1;i<=n;i++) 
        { 
            for(j=1;j<=k;j++) 
            { 
                scanf("%d",&a[i][j]); 
            } 
        } 
        for(i=1;i<=m;i++) 
        { 
            for(j=1;j<=k;j++) 
            { 
                scanf("%d",&b[i][j]); 
            } 
        } 
        for(i=1;i<=k;i++) 
        { 
            for(j=1;j<=n;j++) 
            { 
                for(u=1;u<=m;u++) 
                { 
                    scanf("%d",&c[i][j][u]); 
                } 
            } 
        } 
        for(i=1;i<=k;i++) 
        { 
            s1=0; s2=0; 
            for(j=1;j<=n;j++) 
            { 
                s1+=a[j][i]; 
            } 
            for(j=1;j<=m;j++) 
            { 
                s2+=b[j][i]; 
            } 
            if(s2<s1) 
            { 
                break; 
            } 
        } 
        if(i!=k+1) 
        { 
            printf("-1\n"); 
            continue; 
        } 
        s=0; 
        for(i=1;i<=k;i++) 
        { 
            memset(cap,0,sizeof(cap)); 
            memset(cost,0,sizeof(cost)); 
            for(j=1;j<=m;j++) 
            { 
                cap[0][j]=b[j][i]; 
            } 
            for(j=1;j<=m;j++) 
            { 
                for(u=1;u<=n;u++) 
                { 
                    cap[j][m+u]=b[j][i]; 
                    cost[j][m+u]=c[i][u][j]; 
                    cost[m+u][j]=-1*c[i][u][j]; 
                } 
            } 
            for(j=1;j<=n;j++) 
            { 
                cap[m+j][m+n+1]=a[j][i]; 
            } 
            sta=0; end=m+n+1; 
            s+=EK(); 
        } 
        printf("%d\n",s); 
    } 
    return 0; 

int bfs() 

    int i,j,base,top,x; 
    base=top=0; 
    for(i=0;i<=end;i++) 
    { 
        dis[i]=INF; 
    } 
    memset(status,0,sizeof(status)); 
    queue[top++]=0; 
    dis[0]=0; 
    status[0]=1; 
    while(base<top) 
    { 
        x=queue[base++]; 
        status[x]=0; 
        for(i=1;i<=end;i++) 
        { 
            if(x!=i&&cap[x][i]) 
            { 
                if(dis[i]>(dis[x]+cost[x][i])) 
                { 
                    pre[i]=x; 
                    dis[i]=(dis[x]+cost[x][i]); 
                    if(!status[i]) 
                    { 
                        status[i]=1; 
                        queue[top++]=i; 
                    } 
                } 
            } 
        } 
    } 
    if(dis[end]==INF) 
    { 
        return -1; 
    }else 
    { 
        return 1; 
    } 

int EK() 

    int i,j,k,s=0; 
    int min=INF; 
    while(1) 
    { 
        k=bfs(); 
        if(k==-1) 
        { 
            break; 
        } 
        for(i=end; i!=0; i=pre[i]) 
        { 
            j=pre[i]; 
            if(min>cap[j][i]) 
            { 
                min=cap[j][i]; 
            } 
        } 
        for(i=end; i!=0; i=pre[i]) 
        { 
            j=pre[i]; 
            s+=cost[j][i]*min; 
            cap[j][i]-=min; 
            cap[i][j]+=min; 
        } 
    } 
    return s; 

#include <stdio.h>
#include <string.h>
#include <math.h>
int a[60][60],b[60][60],c[60][60][60];
int cost[100][200],cap[100][200],dis[200];
int queue[1000000],pre[200],status[200];
int INF=0x7fffffff,sta,end;
int main()
{
    int EK();
    int i,j,n,m,s,t,k,u,s1,s2;
    while(scanf("%d %d %d",&n,&m,&k)!=EOF)
    {
        if(n==0&&m==0&&k==0)
        {
            break;
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=k;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=k;j++)
            {
                scanf("%d",&b[i][j]);
            }
        }
        for(i=1;i<=k;i++)
        {
            for(j=1;j<=n;j++)
            {
                for(u=1;u<=m;u++)
                {
                    scanf("%d",&c[i][j][u]);
                }
            }
        }
        for(i=1;i<=k;i++)
        {
            s1=0; s2=0;
            for(j=1;j<=n;j++)
            {
                s1+=a[j][i];
            }
            for(j=1;j<=m;j++)
            {
                s2+=b[j][i];
            }
            if(s2<s1)
            {
                break;
            }
        }
        if(i!=k+1)
        {
            printf("-1\n");
            continue;
        }
        s=0;
        for(i=1;i<=k;i++)
        {
            memset(cap,0,sizeof(cap));
            memset(cost,0,sizeof(cost));
            for(j=1;j<=m;j++)
            {
                cap[0][j]=b[j][i];
            }
            for(j=1;j<=m;j++)
            {
                for(u=1;u<=n;u++)
                {
                    cap[j][m+u]=b[j][i];
                    cost[j][m+u]=c[i][u][j];
                    cost[m+u][j]=-1*c[i][u][j];
                }
            }
            for(j=1;j<=n;j++)
            {
                cap[m+j][m+n+1]=a[j][i];
            }
            sta=0; end=m+n+1;
            s+=EK();
        }
        printf("%d\n",s);
    }
    return 0;
}
int bfs()
{
    int i,j,base,top,x;
    base=top=0;
    for(i=0;i<=end;i++)
    {
        dis[i]=INF;
    }
    memset(status,0,sizeof(status));
    queue[top++]=0;
    dis[0]=0;
    status[0]=1;
    while(base<top)
    {
        x=queue[base++];
        status[x]=0;
        for(i=1;i<=end;i++)
        {
            if(x!=i&&cap[x][i])
            {
                if(dis[i]>(dis[x]+cost[x][i]))
                {
                    pre[i]=x;
                    dis[i]=(dis[x]+cost[x][i]);
                    if(!status[i])
                    {
                        status[i]=1;
                        queue[top++]=i;
                    }
                }
            }
        }
    }
    if(dis[end]==INF)
    {
        return -1;
    }else
    {
        return 1;
    }
}
int EK()
{
    int i,j,k,s=0;
    int min=INF;
    while(1)
    {
        k=bfs();
        if(k==-1)
        {
            break;
        }
        for(i=end; i!=0; i=pre[i])
        {
            j=pre[i];
            if(min>cap[j][i])
            {
                min=cap[j][i];
            }
        }
        for(i=end; i!=0; i=pre[i])
        {
            j=pre[i];
            s+=cost[j][i]*min;
            cap[j][i]-=min;
            cap[i][j]+=min;
        }
    }
    return s;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved