题目链接
2192. 运输问题
$W$ 公司有 $m$ 个仓库和 $n$ 个零售商店。
第 $i$ 个仓库有 $a_i$ 个单位的货物;第 $j$ 个零售商店需要 $b_j$ 个单位的货物。
货物供需平衡,即$\sum\limits_{i=1}^{m}a_i=\sum\limits_{j=1}^{n}b_j$。
从第 $i$ 个仓库运送每单位货物到第 $j$ 个零售商店的费用为 $c_{ij}$。
试设计一个将仓库中所有货物运送到零售商店的运输方案。
对于给定的 $m$ 个仓库和 $n$ 个零售商店间运送货物的费用,计算最优运输方案和最差运输方案。
输入格式
第 $1$ 行有 $2$ 个正整数 $m$ 和 $n$,分别表示仓库数和零售商店数。
接下来的一行中有 $m$ 个正整数 $a_i$,表示第 $i$ 个仓库有 $a_i$ 个单位的货物。
再接下来的一行中有 $n$ 个正整数 $b_j$,表示第 $j$ 个零售商店需要 $b_j$ 个单位的货物。
接下来的 $m$ 行,每行有 $n$ 个整数,表示从第 $i$ 个仓库运送每单位货物到第 $j$ 个零售商店的费用 $c_{ij}$。
输出格式
第一行输出最少运输费用。
第二行输出最多运输费用。
数据范围
$1 \le m \le 100$,
$1 \le n \le 50$,
$1 \le a_i \le 30000$,
$1 \le b_i \le 60000$,
$1 \le c_{ij} \le 1000$
输入样例:
2 3
220 280
170 120 210
77 39 105
150 186 122
输出样例:
48500
69140
解题思路
费用流
建图:建立源点 $s$ 和汇点 $t$,源点向仓库连边,容量为仓库库存,费用为 $0$,所有商店向汇点连边,容量为商店需求,费用为 $0$,所有仓库向商店连边,容量足够大,费用为从商店将货物移到该商店的单价,最后求解从 $s$ 到 $t$ 的费用流即为所求,$\color{red}{为什么?}$不能发现,最后一定是满流的状态,从仓库流到商店的流量表示向该商店出多少货,费用即为单价乘以流的量,可行流和实际问题是一一对应的
设最大流为 $f$,$k$ 为 spfa
算法常数,则:
- 时间复杂度:$O(knmf)$
代码
// Problem: 运输问题
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2194/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int N=155,M=(5005+N)*2,inf=1e9;
int m,n,S,T;
int h[N],e[M],ne[M],f[M],w[M],idx;
int d[N],incf[N],q[N],pre[N];
bool st[N];
void add(int a,int b,int c,int d)
{
e[idx]=b,f[idx]=c,w[idx]=d,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,w[idx]=-d,ne[idx]=h[b],h[b]=idx++;
}
bool spfa()
{
int hh=0,tt=1;
memset(d,0x3f,sizeof d);
memset(incf,0,sizeof incf);
q[0]=S,d[S]=0,incf[S]=inf;
while(hh!=tt)
{
int x=q[hh++];
if(hh==N)hh=0;
st[x]=false;
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]>d[x]+w[i]&&f[i])
{
d[y]=d[x]+w[i];
pre[y]=i;
incf[y]=min(incf[x],f[i]);
if(!st[y])
{
q[tt++]=y;
if(tt==N)tt=0;
st[y]=true;
}
}
}
}
return incf[T]>0;
}
int EK()
{
int cost=0;
while(spfa())
{
int t=incf[T];
cost+=t*d[T];
for(int i=T;i!=S;i=e[pre[i]^1])
f[pre[i]]-=t,f[pre[i]^1]+=t;
}
return cost;
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d",&m,&n);
S=0,T=m+n+1;
int a,b,c;
for(int i=1;i<=m;i++)
{
scanf("%d",&a);
add(S,i,a,0);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b);
add(i+m,T,b,0);
}
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&c);
add(i,j+m,inf,c);
}
printf("%d\n",EK());
for(int i=0;i<idx;i+=2)
{
f[i]+=f[i^1],f[i^1]=0;
swap(w[i],w[i^1]);
}
printf("%d",-EK());
return 0;
}