POJ

来源:互联网 发布:中国高速铁路 知乎 编辑:程序博客网 时间:2024/06/14 19:39
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

终于会写费用流了TUT。。

题目大意:给你n,m,k三个数分别代表 商店,供货商,物品的数量。  然后n×k的矩阵,代表商店i对第j种物品的需求,  m×k的矩阵,代表供货商i提供j种物品的数量,  k组n×m的矩阵代表供货商到商店的运费价格。
 然后建图就可以了。

k个物品是独立的,所以每次求一个物品的费用流,最后把它们相加。建立源点->m个供货商,容量为供货商的提供量,费用为0,m个供货商->n个商店,容量为无限大,费用为n->m的费用,n个商店->汇点,容量为商店对物品的需求量,费用为0。

跑k次最短路就好了。 再加上一个判断条件,如果供不应求的话要输出-1 。。被坑了。= =

#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int MAXN=10000;const int MAXM=100000;const int INF=0x3f3f3f3f;const int NN=160;struct node{    int u,v,cap,next;    int flow,cost;}eage[MAXM];int head[MAXN];int dis[MAXN];int pre[MAXN];bool vis[MAXN];int N;int n,m,k;int top;int need[NN];int have[NN];int a[NN][NN];int b[NN][NN];int c[NN][NN];void Init(int n){    N=n;    top=0;    memset(head,-1,sizeof(head));}void Add(int u,int v,int cap,int cost){    eage[top].v=v;    eage[top].cap=cap;    eage[top].cost=cost;    eage[top].flow=0;    eage[top].next=head[u];    head[u]=top++;    eage[top].v=u;    eage[top].cap=0;    eage[top].cost=-cost;    eage[top].flow=0;    eage[top].next=head[v];    head[v]=top++;}bool spfa(int s,int t){    queue<int>q;    for(int i=0;i<=N;i++)    {        dis[i]=INF;        vis[i]=0;        pre[i]=-1;    }    dis[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=eage[i].next)        {            int v=eage[i].v;            if(eage[i].cap>eage[i].flow&&dis[v]>dis[u]+eage[i].cost)            {                dis[v]=dis[u]+eage[i].cost;                pre[v]=i;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    if(pre[t]==-1)return 0;    else return 1;}int minCostMaxflow(int s,int t,int &cost){    int flow=0;    cost=0;    while(spfa(s,t))    {        int Min=INF;        for(int i=pre[t];i!=-1;i=pre[eage[i^1].v])        {            if(Min>eage[i].cap-eage[i].flow)            Min=eage[i].cap-eage[i].flow;        }        for(int i=pre[t];i!=-1;i=pre[eage[i^1].v])        {            eage[i].flow+=Min;            eage[i^1].flow-=Min;            cost+=eage[i].cost*Min;        }        flow+=Min;    }    return flow;}int main(){    while(~scanf("%d%d%d",&n,&m,&k)&&n&&m&&k)    {        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",&a[i][j]);                need[j]+=a[i][j];            }        }        for(int i=1;i<=m;i++)//供货商提供        {            for(int j=1;j<=k;j++)            {                scanf("%d",&b[i][j]);                have[j]+=b[i][j];            }        }        int flag=0;        for(int i=1;i<=k;i++)//供不应求        {            if(have[i]<need[i])            {                flag=1;                break;            }        }        int ans=0;        int maxflow=0;        for(int K=1;K<=k;K++)//j->i的价格        {            Init(n+50);            for(int i=1;i<=n;i++)            {                for(int j=1;j<=m;j++)                {                    scanf("%d",&c[i][j]);                    Add(j,i+m,INF,c[i][j]);                }            }            if(flag)continue;            for(int i=1;i<=n;i++)            {                Add(m+i,n+m+1,a[i][K],0);            }            for(int i=1;i<=m;i++)            {                Add(0,i,b[i][K],0);            }            int tmp=0;            maxflow+=minCostMaxflow(0,n+m+1,tmp);            if(need[K]>maxflow)flag=1;//如果第K个物品的需求量大于最大流的话也算供不应求。。。            ans+=tmp;            //printf("cost = %d\n",tmp);        }        if(flag)printf("-1\n");        else printf("%d\n",ans);    }    return 0;}


原创粉丝点击