Minimum Cost (最小费用)
2018-01-10 17:04
232 查看
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
Sample Output
题意:N个商店,M个仓库,每个仓库可能有K种商品,每次仓库向不同的商店供应货物所需的费用不同;第一行是N,M,K;接下来N行每行K个数,代表每个商店所需ki种商品的个数;又有M行每行K个数,代表仓库有ki种商品的个数;最后是有k个n*m的矩阵,每个矩阵中cij表示从j仓库往i商店送ki种商品,需要cij/单位的费用;
刚开始想着是建一个图,源点->仓库的每种商品->商店的商品->汇点……一直超时— —||;
应该是将每种商品建一个图,再进行叠加
源点->仓库的第ki中商品,cost=0,cap=仓库中ki种商品的个数;仓库的第ki中商品->商店的第ki种商品,cost=题中所给的数据,cap=inf;商店中的第ki种商品->汇点,cost=0,cap=商店所需的商品个数;
#include<cstdio>
#
4000
include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<string>
using namespace std;
const int MAXN=1000+10;
const int inf=0x3f3f3f3f3f;
struct node
{
int v,cap,flow,cost;
int next;
} Edge[MAXN*MAXN];
struct node1
{
int sum1,sum2;
}z[MAXN];
int edn,head[MAXN];
int dis[MAXN],book[MAXN];
int pre[MAXN];
int aa[MAXN];
int N;
int n,m,k;
void AddEdge(int u,int v,int cost,int cap)
{
Edge[edn].v=v,Edge[edn].cost=cost,Edge[edn].cap=cap,Edge[edn].flow=0;
Edge[edn].next=head[u],head[u]=edn++;
Edge[edn].v=u,Edge[edn].cost=-cost,Edge[edn].cap=0,Edge[edn].flow=0;;
Edge[edn].next=head[v],head[v]=edn++;
}
bool spfa(int &flow,int &cost)
{
memset(dis,inf,sizeof(dis));
memset(book,0,sizeof(book));
memset(pre,-1,sizeof(pre));
dis[0]=0;
book[0]=1;
aa[0]=inf;
queue<int>s;
s.push(0);
while(!s.empty())
{
int u=s.front();
s.pop();
book[u]=0;
for(int i=head[u]; i!=-1; i=Edge[i].next)
{
int v=Edge[i].v;
if(Edge[i].cap>Edge[i].flow&&dis[v]>dis[u]+Edge[i].cost)
{
dis[v]=dis[u]+Edge[i].cost;
pre[v]=i;
aa[v]=min(aa[u],Edge[i].cap-Edge[i].flow);
if(!book[v])
{
book[v]=1;
s.push(v);
}
}
}
}
if(dis[N+1]==inf) return false;
flow+=aa[N+1];
cost+=dis[N+1]*aa[N+1];
for(int i=pre[N+1]; i!=-1; i=pre[Edge[i^1].v])
{
Edge[i].flow+=aa[N+1];
Edge[i^1].flow-=aa[N+1];
}
return true;
}
int mincost(int sum)
{
int flow=0,cost=0;
while(spfa(flow,cost));
if(flow==sum) return cost;
return 0;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(!(n+m+k)) break;
int ans=0;
N=n+m;
int a[n+2][k+2],b[m+2][k+2];
int c;
for(int i=1;i<=k;i++)z[i].sum1=z[i].sum2=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
scanf("%d",&a[i][j]);
z[j].sum1+=a[i][j];
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=k;j++)
{
scanf("%d",&b[i][j]);
z[j].sum2+=b[i][j];
}
}
int flag=0,sum;
for(int kk=1;kk<=k;kk++)
{
memset(head,-1,sizeof(head));
edn=sum=0;
for(int j=1;j<=m;j++)AddEdge(0,j,0,b[j][kk]);//源点->仓库
for(int i=1;i<=n;i++)//商店
{
for(int j=1;j<=m;j++)//仓库
{
scanf("%d",&c);
AddEdge(j,m+i,c,inf);//仓库->商店
}
AddEdge(m+i,N+1,0,a[i][kk]);//商店->汇点
sum+=a[i][kk];
}
int co;
if(z[kk].sum2<z[kk].sum1)//供应<需求,不能完成供货
flag=1;
if(!flag)
{
if(co=mincost(sum)) ans+=co;
else flag=1;
}
}
if(!flag) printf("%d\n",ans);
else printf("-1\n");
}
}
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
题意:N个商店,M个仓库,每个仓库可能有K种商品,每次仓库向不同的商店供应货物所需的费用不同;第一行是N,M,K;接下来N行每行K个数,代表每个商店所需ki种商品的个数;又有M行每行K个数,代表仓库有ki种商品的个数;最后是有k个n*m的矩阵,每个矩阵中cij表示从j仓库往i商店送ki种商品,需要cij/单位的费用;
刚开始想着是建一个图,源点->仓库的每种商品->商店的商品->汇点……一直超时— —||;
应该是将每种商品建一个图,再进行叠加
源点->仓库的第ki中商品,cost=0,cap=仓库中ki种商品的个数;仓库的第ki中商品->商店的第ki种商品,cost=题中所给的数据,cap=inf;商店中的第ki种商品->汇点,cost=0,cap=商店所需的商品个数;
#include<cstdio>
#
4000
include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<string>
using namespace std;
const int MAXN=1000+10;
const int inf=0x3f3f3f3f3f;
struct node
{
int v,cap,flow,cost;
int next;
} Edge[MAXN*MAXN];
struct node1
{
int sum1,sum2;
}z[MAXN];
int edn,head[MAXN];
int dis[MAXN],book[MAXN];
int pre[MAXN];
int aa[MAXN];
int N;
int n,m,k;
void AddEdge(int u,int v,int cost,int cap)
{
Edge[edn].v=v,Edge[edn].cost=cost,Edge[edn].cap=cap,Edge[edn].flow=0;
Edge[edn].next=head[u],head[u]=edn++;
Edge[edn].v=u,Edge[edn].cost=-cost,Edge[edn].cap=0,Edge[edn].flow=0;;
Edge[edn].next=head[v],head[v]=edn++;
}
bool spfa(int &flow,int &cost)
{
memset(dis,inf,sizeof(dis));
memset(book,0,sizeof(book));
memset(pre,-1,sizeof(pre));
dis[0]=0;
book[0]=1;
aa[0]=inf;
queue<int>s;
s.push(0);
while(!s.empty())
{
int u=s.front();
s.pop();
book[u]=0;
for(int i=head[u]; i!=-1; i=Edge[i].next)
{
int v=Edge[i].v;
if(Edge[i].cap>Edge[i].flow&&dis[v]>dis[u]+Edge[i].cost)
{
dis[v]=dis[u]+Edge[i].cost;
pre[v]=i;
aa[v]=min(aa[u],Edge[i].cap-Edge[i].flow);
if(!book[v])
{
book[v]=1;
s.push(v);
}
}
}
}
if(dis[N+1]==inf) return false;
flow+=aa[N+1];
cost+=dis[N+1]*aa[N+1];
for(int i=pre[N+1]; i!=-1; i=pre[Edge[i^1].v])
{
Edge[i].flow+=aa[N+1];
Edge[i^1].flow-=aa[N+1];
}
return true;
}
int mincost(int sum)
{
int flow=0,cost=0;
while(spfa(flow,cost));
if(flow==sum) return cost;
return 0;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(!(n+m+k)) break;
int ans=0;
N=n+m;
int a[n+2][k+2],b[m+2][k+2];
int c;
for(int i=1;i<=k;i++)z[i].sum1=z[i].sum2=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
scanf("%d",&a[i][j]);
z[j].sum1+=a[i][j];
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=k;j++)
{
scanf("%d",&b[i][j]);
z[j].sum2+=b[i][j];
}
}
int flag=0,sum;
for(int kk=1;kk<=k;kk++)
{
memset(head,-1,sizeof(head));
edn=sum=0;
for(int j=1;j<=m;j++)AddEdge(0,j,0,b[j][kk]);//源点->仓库
for(int i=1;i<=n;i++)//商店
{
for(int j=1;j<=m;j++)//仓库
{
scanf("%d",&c);
AddEdge(j,m+i,c,inf);//仓库->商店
}
AddEdge(m+i,N+1,0,a[i][kk]);//商店->汇点
sum+=a[i][kk];
}
int co;
if(z[kk].sum2<z[kk].sum1)//供应<需求,不能完成供货
flag=1;
if(!flag)
{
if(co=mincost(sum)) ans+=co;
else flag=1;
}
}
if(!flag) printf("%d\n",ans);
else printf("-1\n");
}
}
相关文章推荐
- POJ 2516 - Minimum Cost 分为几次做最小费用最大流...
- POJ2516 Minimum Cost 最大流最小费用典型题目
- POJ2516 Minimum Cost 最大流最小费用典型题目
- POJ 2516 Minimum Cost 最大流的最小费用(不要拆点!)
- poj 2516 Minimum Cost 【最小费用最大流】【求解K种物品的最小费用,独立求解累加每个结果】
- HDU 3072 Intelligence System 传递的最小费用
- hdu 3488 Tour (有向环最小权值覆盖,费用流)
- (最小费用) POJ 2135 Farm Tour
- 优先队列/POJ3253,哈夫曼树(求最小木板分割费用)
- Regionals 2015 >> Asia - Tehran >> 7530 - Cafebazaar【二分图最佳匹配】【最小费用可行流】
- 最小费用最大流模板 poj 2159 模板水题
- (ssl 1697)最小乘车费用
- 最小费用网络流
- POJ2195 Going Home(网络流,最小费用流,最小费用路算法)
- hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流
- 动态规划--二维费用背包--最大最小价值--hdu4968 Improving the GPA
- 字符串变换最小费用
- 最小费用最大流模板
- POJ 3270 Cow Sorting(置换最小费用)
- HDU3488Tour(KM最小费用圈覆盖)