POJ 2516 Minimum Cost (multi-source multi-sink fee stream)

Source: Internet
Author: User
Tags printf

Minimum Cost

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 15764 Accepted: 5514

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 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, and you should tell how to arrange the goods Supply to minimize the total cost of transport.

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 this supply place. 

then come K integer matrices (E Ach with the size N * M), 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 shopkeepers, print in one line an integer, whic H 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

0 0 0

Sample Output

4
-1
Test instructions: There are n customers, M warehouses, K-Items

First give the number of K items each customer needs, M warehouse K items inventory, each item per warehouse to each customer's freight, ask the total cost of the minimum is how much. If not all full, output-1

Idea: The topic according to the article gives a n*m matrix, it is easy to think of enumerating k items, here can also really break down all the items, because each other is not affected by the respective answers, here is the freight per item instead of the distance (the same distance transporting an item is not the same as transporting two kinds of goods, Even if the capacity is legal), so the edge enumeration side to calculate the minimum freight per item, and finally add up, if there is an inability to reach full flow is illegal, mark it, and finally judged

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Cmath> #include <queue> using namespace std;
#define N #define M 30010 #define INF 0x3f3f3f3f struct Edge {int from,to,next,cap,cost;///start, end point, same starting point next edge, residual flow, cost
} Edge[m*7];
int cnt,head[n],sum[n];
int vis[n],d[n],pp[n],num[n][n],ma[n][n],a[n][n];
    int sumflow;///maximum flow sum void init () {cnt=0;
memset (head,-1,sizeof (head));
    } void Addedge (int from,int to,int cap,int cost) {edge[cnt].from=from;
    Edge[cnt].to=to;
    Edge[cnt].cost=cost;
    Edge[cnt].cap=cap;
    Edge[cnt].next=head[from];
    head[from]=cnt++;
    Edge[cnt].from=to;
    Edge[cnt].to=from;
    Edge[cnt].cost=-cost;
    Edge[cnt].cap=0;
    Edge[cnt].next=head[to];
    head[to]=cnt++;///save reverse side} int SPFA (int s,int t,int n) {queue<int>q;
    memset (vis,0,sizeof (VIS));
    memset (pp,-1,sizeof (PP));///pp[i] represents the number of edges on the shortest path ending with I for (int i=0; i<=n; i++) D[i]=inf;d[s]=0;
    Vis[s]=1;
    Q.push (s);
        while (!q.empty ()) {int U=q.front ();
        Q.pop ();
        vis[u]=0;
            for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].to;
                if (edge[i].cap>0&&d[v]>d[u]+edge[i].cost) {d[v]=d[u]+edge[i].cost;
                Pp[v]=i;
                    if (!vis[v]) {vis[v]=1;
                Q.push (v);
}}}} if (D[t]==inf) return 0;///cannot find a route to the end of return 1;
    } int MCMF (int s,int t,int n) {int mincost=0,minflow,flow=0;///minimum cost, minimum traffic in path, total traffic while (SPFA (s,t,n))///Find current Shortest
        {minflow=inf+1; for (int i=pp[t]; i!=-1; I=pp[edge[i].from]) minflow=min (MINFLOW,EDGE[I].CAP);///Find the smallest flow from the path FLOW+=MINFL ow;///total traffic plus minimum flow for (int i=pp[t]; i!=-1; I=pp[edge[i].from]) {edge[i].cap-=minflow;///current edge minus minimum flow Volume edge[i^1].cap+=minflow;///Reverse Edge Plus minimum traffic} mincost+=d[t]*minflow;///the minimum cost equals the path and the traffic per path (after how many times)} Sumflow=flow;
return mincost;
    } int main () {int n,m,k;
        while (~SCANF ("%d%d", &n,&m,&k) && (n+m+k)) {memset (sum,0,sizeof (sum)); for (int. I=1; i<=n; i++) for (int j=1; j<=k; J + +) {scanf ("%d", &num[i][j])
                ;
            SUM[J]+=NUM[I][J];
        } for (int i=1; i<=m; i++) for (int j=1; j<=k; j + +) scanf ("%d", &ma[i][j]);
        int flag=0,s=0,t=n+m+1,ans=0;
            for (int l=1; l<=k; l++) {init ();
            for (int. I=1; i<=n; i++) for (int j=1; j<=m; j + +) scanf ("%d", &a[i][j]);
            if (flag) continue;
            for (int i=1; i<=m; i++) Addedge (s,i,ma[i][l],0);
      for (int i=1, i<=m; i++) for (int j=1; j<=n; j + +)              Addedge (I,m+j,inf,a[j][i]);
            for (int i=m+1; i<=m+n; i++) Addedge (i,t,num[i-m][l],0);
            sumflow=0;
            ANS+=MCMF (s,t,t);
        if (sumflow<sum[l]) flag=1;
        } if (flag) printf (" -1\n");
    else printf ("%d\n", ans);
} 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.