Minimum Cost
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 15546 Accepted: 5393
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
题意:
有n个商人,m个供货点,k种货物,现已知每个商人对每种货物的需求量,每个供货点各种货物的数量,以及每种货物从每个供货点到每个商人处的单位运输费用,求若能满足所有商人的需求,最少要花多少钱在运输上,若不能满足所有商人的需求,就输出-1。
题解:
一开始想的是拆点建立一张大网,后来算了算感觉复杂度不对劲,所以选择了拆网,根据贪心可知,在满足所有商人的需求的情况下,对k种货物依次求最小费用,加起来的和就是总的最小费用,那么将原图拆为k张网,每张网有源汇点和n个商人点以及m个供货点。
对于第x张网,从源点到每个供货点依次连一条上限为该供货点x商品数量的边,cost为0;从每个商人点连一条上限为该商人对x商品需求数量的边,cost为0;然后对于第j个供货点与第i个商人点,连一条无上限的单位费用为cost[x][i][j]的边。最后跑最小费用最大流,将流的大小和cost都记录下来。
在跑完k次最小费用最大流以后,将总的流大小和所有商人对所有货物的需求数量进行对比,如果相等就说明有解,则输出cost,若不等则输出-1。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
int n,m,k;
int num1[55][55],num2[55][55],maze[55][55][55];
const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
N = n;
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i = 0; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i].cost )
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -1)return false;
else return true;
}
//返回的是最大流,cost存的是最小费用
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[edge[i^1].to])
{
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m >> k && n!=0)
{
int total1=0,total2=0,out=0,out1=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=k; j++)
{
cin >> num2[i][j];
total1+=num2[i][j];
}
for(int i=1; i<=m; i++)
for(int j=1; j<=k; j++)
cin >> num1[i][j];
for(int i=1; i<=k; i++)
for(int j=1; j<=n; j++)
for(int x=1; x<=m; x++)
cin >> maze[i][j][x];
for(int x=1;x<=k;x++)
{
init(n+m+2);
for(int i=1;i<=m;i++)
addedge(0,i,num1[i][x],0);
for(int i=m+1;i<=n+m;i++)
addedge(i,m+n+1,num2[i-m][x],0);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
addedge(j,i+m,num1[j][x],maze[x][i][j]);
total2+=minCostMaxflow(0,m+n+1,out1);
out+=out1;
}
if(total1==total2)
cout << out << endl;
else
cout << -1 << endl;
}
return 0;
}