POJ 2516 Minimum Cost

来源:互联网 发布:jenkins 源码管理 编辑:程序博客网 时间:2024/05/29 21:16
点击打开链接

Minimum Cost
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 12585 Accepted: 4262

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 10 1 11 2 21 0 11 2 31 1 12 1 11 1 132200 0 0

Sample Output

4-1

Source

POJ Monthly--2005.07.31, Wang Yijie

题意是说:有 M 个供应商,N 个店主,K 个物品, 知道了每个供应商对每个物品的供应量,每个店主对每个物品的需求量,和每个供应商把每个物品送到每个店主所需要花的费用。问已有的货物是否可以满足所有供应商的需求,如果满足,求出供应所需花费的最小费用。
任何网络流的关键在于建图:
建立一个超级源点,与所有的商店相连,费用为0,流量为商店存的物品数。建立一个超级汇点,与所有供应商相连,费用为0,流量为供应商所需要的物品数。所有的商店与所有供应商相连,费用为题目所给的,流量为无穷大。

#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<cstdio>using namespace std;const int MAXN=207;const int inf=1<<29;int pre[MAXN];          // pre[v] = k:在增广路上,到达点v的边的编号为kint dis[MAXN];          // dis[u] = d:从起点s到点u的路径长为dint vis[MAXN];         // inq[u]:点u是否在队列中int path[MAXN];int head[MAXN];int NE,tot,ans,max_flow;struct node{    int u,v,cap,cost,next;} Edge[10007];void addEdge(int u,int v,int cap,int cost){    Edge[NE].u=u;    Edge[NE].v=v;    Edge[NE].cap=cap;    Edge[NE].cost=cost;    Edge[NE].next=head[u];    head[u]=NE++;    Edge[NE].v=u;    Edge[NE].u=v;    Edge[NE].cap=0;    Edge[NE].cost=-cost;    Edge[NE].next=head[v];    head[v]=NE++;}void init(){    memset(head,-1,sizeof(head));    NE=ans=max_flow=0;}int SPFA(int s,int t)                   //  源点为0,汇点为sink。{    int i;    for(i=s;i<=t;i++) dis[i]=inf;    memset(vis,0,sizeof(vis));    memset(pre,-1,sizeof(pre));    dis[s] = 0;    queue<int>q;    q.push(s);    vis[s] =1; while(!q.empty())        //  这里最好用队列,有广搜的意思,堆栈像深搜。    {        int u =q.front();        q.pop();        for(i=head[u]; i!=-1;i=Edge[i].next)        {            int v=Edge[i].v;            if(Edge[i].cap >0&& dis[v]>dis[u]+Edge[i].cost)            {                dis[v] = dis[u] + Edge[i].cost;                pre[v] = u;                path[v]=i;                if(!vis[v])                {                    vis[v] =1;                    q.push(v);                }            }        }        vis[u] =0;    }    if(pre[t]==-1)        return 0;    return 1;}void end(int s,int t){    int u, sum = inf;    for(u=t; u!=s; u=pre[u])    {        sum = min(sum,Edge[path[u]].cap);    }    max_flow+=sum;                          //记录最大流    for(u = t; u != s; u=pre[u])    {        Edge[path[u]].cap -= sum;        Edge[path[u]^1].cap += sum;        ans += sum*Edge[path[u]].cost;     //  cost记录的为单位流量费用,必须得乘以流量。    }}int main(){    int i,j,n,s,t,m,k,shop[207][207],supply[207][207];    while(scanf("%d%d%d",&n,&m,&k),n|m|k)    {        int res;        //int shop[MAXN][MAXN];        //supply[MAXN][MAXN];        memset(head,-1,sizeof(head));        NE=ans=max_flow=s=res=0;        int t=n+m+1,flag=1;        for(int i=1;i<=n;i++)            for(int j=1;j<=k;j++)                scanf("%d",&shop[i][j]);        for(int i=1;i<=m;i++)            for(int j=1;j<=k;j++)                scanf("%d",&supply[i][j]);        int c;        for(int a=1;a<=k;a++)        {            init();            for(int i=1;i<=n;i++)                for(int j=1;j<=m;j++)                {                    scanf("%d",&c);                    addEdge(j,i+m,inf,c);                }            int fk=0;            for(int i=1;i<=m;i++)                addEdge(s,i,supply[i][a],0);            for(int i=1;i<=n;i++)                addEdge(i+m,t,shop[i][a],0),fk+=shop[i][a];            while(SPFA(s,t))end(s,t);            res+=ans;            if(max_flow!=fk)flag=0;        }        if(flag)printf("%d\n",res);        else printf("-1\n");    }    return 0;}


原创粉丝点击