W 公司有 m 个仓库和 n 个零售商店。第 i 个仓库有 ai 个单位的货物;第 j 个零售商店需要 bj 个单位的货物。货物供需平衡,即
第 1 行有 2 个正整数 m和 n,分别表示仓库数和零售商店数。接下来的一行中有 m个正整数 ai ,1≤i≤m,表示第 i 个仓库有 ai 个单位的货物。再接下来的一行中有 n 个正整数 bj ,1≤j≤n,表示第 j 个零售商店需要 bj 个单位的货物。接下来的 m行,每行有 n 个整数,表示从第 i 个仓库运送每单位货物到第 j 个零售商店的费用 cij 。
输出计算出的最少运输费用和最多运输费用。
2 3
220 280
170 120 210
77 39 105
150 186 122
48500
69140
建图:
建立附加源S,附加汇T。
1.S向每个仓库连接一条容量为仓库货物数量,费用为0的边。
2.每个商店向T连接一条容量为商店需要货物数量,费用为0的边。
3.每个仓库和每个商店之间连接一条容量无穷大,费用为运送费用的边。
求出最小(大)费用最大流就是答案。
#include
#include
#include
#include
using namespace std;
const int N = 1000 + 10, M = 1000000 + 10, inf = 0x3f3f3f3f;
struct Edge{
int fr, to, cap, flow, cost;
}edg[M];
int nxt[M], hd[N], tot;
int n, m;
int s, t;
int q[N], inq[N], p[N], a[N], d[N];
int wh[N], sp[N], val[N][N];
void insert(int u, int v, int w, int x){
edg[tot].fr = u, edg[tot].to = v, edg[tot].cap = w, edg[tot].flow = 0, edg[tot].cost = x;
nxt[tot] = hd[u]; hd[u] = tot;
tot++;
edg[tot].fr = v, edg[tot].to = u, edg[tot].cap = 0, edg[tot].flow = 0, edg[tot].cost = -x;
nxt[tot] = hd[v]; hd[v] = tot;
tot++;
}
void init(){
scanf("%d%d", &m, &n);
s = 0, t = n + m + 1;
int w;
for(int i = 1; i <= m; i++)
scanf("%d", &wh[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &sp[i]);
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &val[i][j]);
}
void build(){
tot = 0;
memset(hd, -1, sizeof(hd));
for(int i = 1; i <= m; i++)
insert(s, i, wh[i], 0);
for(int i = 1; i <= n; i++)
insert(i + m, t, sp[i], 0);
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
insert(i, m + j, inf, val[i][j]);
}
bool spfa1(int &fl, int &cst){
for(int i = s; i <= t; i++) d[i] = inf;
p[s] = 0; a[s] = inf; d[s] = 0;
int head = 0, tail = 1;
q[0] = s; inq[s] = 1;
while(head != tail){
int u = q[head++]; if(head == 1001) head = 0;
inq[u] = 0;
for(int i = hd[u]; i >= 0; i = nxt[i]){
Edge &e = edg[i];
if(d[e.to] > d[u] + e.cost && e.cap > e.flow){
d[e.to] = d[u] + e.cost;
p[e.to] = i;
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]){
q[tail++] = e.to; if(tail == 1001) tail = 0;
inq[e.to] = 1;
}
}
}
}
if(d[t] == inf) return false;
fl += a[t];
cst += a[t] * d[t];
int u = t;
while(u != s){
edg[p[u]].flow += a[t];
edg[p[u]^1].flow -= a[t];
u = edg[p[u]].fr;
}
return true;
}
bool spfa2(int &fl, int &cst){
for(int i = s; i <= t; i++) d[i] = -inf;
p[s] = 0; a[s] = inf; d[s] = 0;
int head = 0, tail = 1;
q[0] = s; inq[s] = 1;
while(head != tail){
int u = q[head++]; if(head == 1001) head = 0;
inq[u] = 0;
for(int i = hd[u]; i >= 0; i = nxt[i]){
Edge &e = edg[i];
if(d[e.to] < d[u] + e.cost && e.cap > e.flow){
d[e.to] = d[u] + e.cost;
p[e.to] = i;
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]){
q[tail++] = e.to; if(tail == 1001) tail = 0;
inq[e.to] = 1;
}
}
}
}
if(d[t] == -inf) return false;
fl += a[t];
cst += a[t] * d[t];
int u = t;
while(u != s){
edg[p[u]].flow += a[t];
edg[p[u]^1].flow -= a[t];
u = edg[p[u]].fr;
}
return true;
}
void work(){
int flow = 0, cost = 0;
build();
while(spfa1(flow, cost));
printf("%d\n", cost);
flow = cost = 0;
build();
while(spfa2(flow, cost));
printf("%d\n", cost);
}
int main(){
freopen("prog817.in", "r", stdin);
freopen("prog817.out", "w", stdout);
init();
work();
return 0;
}