Poj2516--minimum Cost (maximum flow min.)

Source: Internet
Author: User
Tags bool min printf

Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In He sale area there is N shopkeepers (marked from 1 to N) which stocks goods from him. Dearboy have 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 what much amount of goods to shopkeepers to Cut down the total cost of transport.

It's known that's the cost of transport one unit goods for different kinds from different supply places to different Shopkee Pers may 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, and 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 all test case contains three integers n, m, K (0 < N, m, K <), which is described above. The next N lines give the shopkeepers ' orders, with each line containing K integers (there integers is belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places ' storage, with all line containing K integers (there integers is 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) and the integer (this integer was belong to (0)) 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 is should not being processed.
Output

For each test case, if Dearboy can satisfy all the needs of the the shopkeepers, print in one line a integer, which is th e 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

Give the number of stores N, supply a few m and item type K
and give each store the number of items it needs.
and give the number of each item that can be supplied per supply.
The k matrix is given to represent the cost from the supply to the store, the line represents the store, and the column represents the supply.

Next is the map, source point 0 to the supply 1~m, no cost, capacity is the number of food supplies can be supplied
Supply 1~m to the store m+1~m+n, the cost is the input of the topic, unlimited capacity
Store m+1~m+n to meeting Point M+n+1, no charge, capacity is the amount of food the store needs
Then for each kind of food, the minimum cost of the maximum flow, and finally add up the minimum cost
Note that the matrix is n*m, but the cost is from M to N, note the conversion

#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 10001 using 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;//Final request is total cost = Unit Price * Quantity}} 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", &am
                    P;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;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.