Poj Training Plan 2516_minimum cost (network stream/fee Stream)

Source: Internet
Author: User

Solution report

Question:
There are n stores, M providers, and a matrix of K commodities </span> N * k, indicating the number of each commodity required by each store; M * k matrix, indicates the number of items owned by each provider and then for each item k, the N * M matrix exists. The J column of the I row indicates the price of shipping a K commodity from the J provider to the I store. If yes, all warehouses can meet the needs of all customers, find the minimum total transportation cost
Ideas:
The question of creating a graph cannot directly build all the information into a graph, because N and M have a relationship with K. If we build a graph like that, we need to split K into M classes, the K type that each warehouse connects to the warehouse, and then connects to N at a cost,
But this is actually not the case. We will find that the relationship between N and K cannot be solved.
If K types of products are processed K times, it is equivalent to one operation.
For each product, create a graph and calculate the minimum cost. If the maximum flow cannot meet the customer's needs, pass.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define inf 0x3f3f3f3fusing namespace std;struct E {    int v,cost,cap,next;} edge[100000];int head[5000],dis[5000],pre[5000],vis[5000],f[5000],m1[55][55],m2[55][55],s,t,n,m,k,cnt,cost,flow;void add(int u,int v,int cost,int cap) {    edge[cnt].v=v;    edge[cnt].cost=cost;    edge[cnt].cap=cap;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].cost=-cost;    edge[cnt].cap=0;    edge[cnt].next=head[v];    head[v]=cnt++;}int _spfa() {    for(int i=s; i<=t; i++) {        dis[i]=inf,vis[i]=pre[i]=f[i]=0;    }    dis[s]=0,vis[s]=1,pre[s]=-1,f[s]=inf;    queue<int>Q;    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].v;            if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) {                pre[v]=i;                dis[v]=dis[u]+edge[i].cost;                f[v]=min(f[u],edge[i].cap);                if(!vis[v]) {                    vis[v]=1;                    Q.push(v);                }            }        }    }    if(dis[t]==inf)return 0;    flow+=f[t];    cost+=f[t]*dis[t];    for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) {        edge[i].cap-=f[t];        edge[i^1].cap+=f[t];    }    return 1;}void mcmf() {    cost=flow=0;    while(_spfa());}int main() {    int i,j,l,a,b,c;    while(~scanf("%d%d%d",&n,&m,&k)) {        int sum[1000];        s=0,t=m+n+1;        if(!n&&!m&&!k)break;        memset(sum,0,sizeof(sum));        memset(m1,0,sizeof(m1));        memset(m2,0,sizeof(m2));        for(i=1; i<=n; i++) {            for(j=1; j<=k; j++) {                scanf("%d",&m1[i][j]);                sum[j]+=m1[i][j];            }        }        for(i=1; i<=m; i++) {            for(j=1; j<=k; j++)                scanf("%d",&m2[i][j]);        }        int f=0;        int ans=0;        for(l=1; l<=k; l++) {            cnt=0;            memset(head,-1,sizeof(head));            memset(edge,0,sizeof(edge));            for(i=1; i<=n; i++) {                for(j=1; j<=m; j++) {                    scanf("%d",&a);                    add(j,m+i,a,inf);                }            }            if(f)continue;            for(i=1; i<=m; i++)                add(s,i,0,m2[i][l]);            for(i=1; i<=n; i++)                add(m+i,t,0,m1[i][l]);            mcmf();            if(flow<sum[l]) {                f=1;            } else ans+=cost;        }        if(f)            printf("-1\n");        else            printf("%d\n",ans);    }}

Minimum Cost
Time limit:4000 Ms   Memory limit:65536 K
Total submissions:13529   Accepted:4633

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 shocould 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 shoshould 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 shoshould 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

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.