poj2516 Minimum Cost

来源:互联网 发布:报表数据的重要性 编辑:程序博客网 时间:2024/06/05 17:10

elijahqi’s blog
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 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

Source
POJ Monthly–2005.07.31, Wang Yijie

题意给定K种商品再给定N个仓库的这k种物品的储存情况 然后再给定m个人 他们对这k种物品的需求情况
现在求问 满足他们的需求我最少需要花费多少的费用 首先可以知道这k种物品是互相独立的 所以我针对每一种物品建边跑一遍费用流 如果中途有一种情况不能满足所有人的需求 就退出说明以后就都不能 输出-1

#include<queue>#include<cstdio>#include<cstring>#include<algorithm>#define N 110#define inf 0x3f3f3f3fusing namespace std;inline char gc(){    static char now[1<<16],*S,*T;    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}    return *S++;}inline int read(){    int x=0;char ch=gc();    while(ch<'0'||ch>'9') ch=gc();    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}    return x; }int n,m,k,num=1,h[N],pre[N],path[N],flag[N],f[N],T,mp[110][110],mp1[110][110];struct node{    int y,z,next,c;}data[N*N*4];inline void insert1(int x,int y,int z,int c){    data[++num].y=y;data[num].z=z;data[num].next=h[x];data[num].c=c;h[x]=num;    data[++num].y=x;data[num].z=0;data[num].next=h[y];data[num].c=-c;h[y]=num;}inline bool spfa(){    queue<int>q;memset(flag,0,sizeof(flag));memset(f,0x3f,sizeof(f));memset(pre,-1,sizeof(pre));flag[0]=1;f[0]=0;q.push(0);    while(!q.empty()){        int x=q.front();q.pop();flag[x]=0;        for (int i=h[x];i;i=data[i].next){            int y=data[i].y,z=data[i].z,c=data[i].c;            if (f[x]+c<f[y]&&z){                f[y]=f[x]+c;pre[y]=x;path[y]=i;                if (!flag[y]) flag[y]=1,q.push(y);            }        }    }if (pre[T]==-1) return 0;else return 1; }int main(){    freopen("poj2516.in","r",stdin);    while(1){        n=read();m=read();k=read();if(!n&&!m&&!k)break;        for (int i=1;i<=n;++i) for (int j=1;j<=k;++j) mp[i][j]=read();        for (int i=1;i<=m;++i) for (int j=1;j<=k;++j) mp1[i][j]=read();        int ans=0;T=n+m+1;bool flag1=0;        for (int i=1;i<=k;++i){            int sum1=0,tmp=0;            for (int j=1;j<=n;++j) sum1+=mp[j][i];            num=1;memset(h,0,sizeof(h));int ans1=0;            for (int j=1;j<=m;++j) insert1(0,j,mp1[j][i],0);            for (int j=1;j<=n;++j) insert1(j+m,T,mp[j][i],0);            for (int j=1;j<=n;++j)                for (int z=1;z<=m;++z) insert1(z,j+m,inf,read());            while(spfa()){                int minn=inf,now=T;                while(now) minn=min(minn,data[path[now]].z),now=pre[now];tmp+=minn;now=T;                while(now) {ans1+=data[path[now]].c*minn;data[path[now]].z-=minn;data[path[now]^1].z+=minn;now=pre[now];}            }if (tmp!=sum1) {flag1=1;continue;} ans+=ans1;        }        if (flag1) printf("-1\n");else printf("%d\n",ans);    }     return 0;}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 300310宜通世纪 300310宜通世纪股吧 宜都 宜都论坛 爱宜都 爱宜都论坛 宜都市 爱宜都网 宜都记 宜都房产网 宜都人 湖北宜都 宜都电大 宜都论坛爱宜都网 我爱宜都网 宜都教育 宜都网 湖北宜都网 宜都地址 宜都县 宜都乡镇 爱宜都网分类信息 宜都市长胡志莉丈夫 宜都15天预报 宜都论坛分类信息网 宜都清江国际酒店 爱宜都论坛分类信息网 宜都论坛招聘信息 宜都招聘信息 湖北宜昌宜都 宜阳 宜阳县 宜阳县邮编 宜阳县宜洛绝老婆事件 宜阳县第一高级中学 宜阳吧 宜阳教育吧 河南宜阳 宜阳招聘 宜阳一高 抚州宜黄