poj 2516

Minimum Cost

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 11530   Accepted: 3892

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種貨物。一個n*k的矩陣代表第i個店主對第j種貨物的需求,接下來的m*k行,表示第i個供貨點第j種的貨物。在接下來的k個n*m個矩陣表示,第j個供貨點供給第i個店的第k種貨物的花費。求最小花費。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 9999999
#define maxn 205
int lx[maxn],ly[maxn];
int w[maxn][maxn];
int Match[maxn],visx[maxn],visy[maxn];
int slack[maxn];
int ans,n,m,k;
int need[maxn][maxn],have[maxn][maxn],cost[maxn][maxn][maxn];
int indexA[maxn],indexB[maxn];
int A,B;
bool findPath(int x)
{
    int tmp;
    visx[x]=1;
    for(int y=1; y<=B; y++)
    {
        if(visy[y])continue;
        if(w[x][y]==lx[x]+ly[y])
        {
            visy[y]=1;
            if(!Match[y]||findPath(Match[y]))
            {
                Match[y]=x;
                return true;
            }
        }
        else
            slack[y]=min(slack[y],w[x][y]-lx[x]-ly[y]);
    }
    return false;
}
void km()
{
    memset(Match,0,sizeof(Match));
    memset(ly,0,sizeof(ly));
    for(int i=1; i<=A; i++)
    {
        lx[i]=INF;
        for(int j=1; j<=B; j++)
            lx[i]=min(lx[i],w[i][j]);
    }
    for(int x=1; x<=A; x++)
    {
        for(int i=1; i<=B; i++)
            slack[i]=INF;
        while(true)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(findPath(x))break;
            int tmp=INF;
            for(int i=1; i<=B; i++)
            {
                if(!visy[i])
                {
                    if(tmp>slack[i])
                        tmp=slack[i];
                }
            }
            if(tmp==INF)return ;
            for(int i=1; i<=A; i++)
                if(visx[i])lx[i]+=tmp;
            for(int i=1; i<=B; i++)
                if(visy[i])ly[i]-=tmp;
        }
    }
}
int main()
{
    int cas;
    while(scanf("%d%d%d",&n,&m,&k))
    {
        if(n==0&&m==0&&k==0)break;
        int a,b,c;
        ans=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=k; j++)
            {
                cin>>need[i][j];
            }
        for(int i=1; i<=m; i++)
            for(int j=1; j<=k; j++)
            {
                cin>>have[i][j];
            }
        for(int i=1; i<=k; i++)
            for(int j=1; j<=n; j++)
                for(int l=1; l<=m; l++)
                    scanf("%d",&cost[i][j][l]);
        int flag1=0;
        for(int i=1; i<=k; i++)
        {
            int sumNeed=0,sumHave=0;
            for(int j=1; j<=n; j++)
                sumNeed+=need[j][i];
            for(int j=1; j<=m; j++)
                sumHave+=have[j][i];
            if(sumNeed>sumHave)
            {
                flag1=1;
                cout<<"-1"<<endl;
                break;
            }
        }
        if(flag1)continue;

        for(int i=1; i<=k; i++)
        {
            A=0;
            B=0;
            for(int j=1; j<=n; j++)
                for(int l=1; l<=need[j][i]; l++)
                    indexA[++A]=j;
            for(int j=1; j<=m; j++)
                for(int l=1; l<=have[j][i]; l++)
                    indexB[++B]=j;
            for(int j=1; j<=A; j++)
                for(int l=1; l<=B; l++)
                    w[j][l]=cost[i][indexA[j]][indexB[l]];
            km();
            //int flag=0;
            for(int i=1; i<=B; i++)
            {
                if(Match[i])
                    ans+=w[Match[i]][i];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

發佈了99 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章