Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 12770 | Accepted: 4349 |
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.
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.
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
应该讲这个题目还是很不错的,首先要审题,审题是个问题,要求最小的费用
很明显是最小费用最大流的知识,直接套模板就是了,难度在于建图。这个题目是
说有n个店主,m个供应点,k种物品,给出每个店主对物品的需求量,给出每个供
应点供应每种物品到每个店的费用,然后就是建图。
该怎么建????以s=0为超级源点,t=n+m+1为超级汇点,s到每个供应处的供应量为供应量的现有值,从供应处到商店的流为inf,就是可以供应无数,而供应的费用
极为路费,从商店到t的路费为0,而流为需要的流,然后求出对于每种商品来说的
最大流和需要的比较,如果小,证明不行无法满足该商品的供应,其实可以直接break,剩下的就是模板了
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
#define N 207
#define inf 1<<29
int pre[N]; // pre[v] = k:在增广路上,到达点v的边的编号为k
int dis[N]; // dis[u] = d:从起点s到点u的路径长为d
int vis[N]; // inq[u]:点u是否在队列中
int path[N];
int head[N];
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,k,n,m,s,t,shop[N][N],supply[N][N];
while(scanf("%d%d%d",&n,&m,&k),n,m,k)
{
int res=0;
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,e;
for(e=1;e<=k;e++)
{
init();
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&c);
addEdge(j,i+m,inf,c);
}
}
int fk=0;
for(i=1;i<=m;i++)
addEdge(s,i,supply[i][e],0);
for(i=1;i<=n;i++)
{
addEdge(i+m,t,shop[i][e],0);
fk+=shop[i][e];
}
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;
}