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 0x3f3f3f3f
using 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;
}