题意
传送门 POJ 2516
题解
最直接的想法,将物品、商店/仓库组成二元组,跑最小费用流,TLE;观察图,只有标识了相同商品的点集才会有直接的连线,于是可以将不同商品的图拆开,缩减图的规模,分别求解最小费用流;对于同一类商品,因为任意商店与仓库相连,只要满足商店需求大于仓库库存,就一定能满足条件,反之输出 − 1 -1 −1。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 0x3f3f3f3f
#define delta 0.85
using namespace std;
#define MAX_V 105
struct edge{
int to, cap, cost, rev;
edge(int to, int cap, int cost, int rev):to(to), cap(cap), cost(cost), rev(rev){}
};
int V;
vector<edge> G[MAX_V];
int h[MAX_V], dist[MAX_V];
int prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, int cost){
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
int min_cost_flow(int s, int t, int f){
int res = 0;
memset(h, 0, sizeof(h));
while(f > 0){
// Dijkstra
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
memset(dist, 0x3f, sizeof(dist));
dist[s] = 0;
que.push(pair<int, int>(0, s));
while(!que.empty()){
pair<int, int> p = que.top(); que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < G[v].size(); i++){
edge &e = G[v][i];
int d2 = dist[v] + e.cost + h[v] - h[e.to];
if(e.cap > 0 && d2 < dist[e.to]){
dist[e.to] = d2;
prevv[e.to] = v;
preve[e.to] = i;
que.push(pair<int, int>(dist[e.to], e.to));
}
}
}
if(dist[t] == INF){
return -1;
}
for(int v = 0; v < V; v++) h[v] += dist[v];
int d = f;
for(int v = t; v != s; v = prevv[v]){
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for(int v = t; v != s; v = prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
void clear_graph(){
for(int v = 0; v < V; v++) G[v].clear();
}
#define MAX_N 50
int N, M, K, F;
int cost[MAX_N][MAX_N][MAX_N];
int od[MAX_N][MAX_N], sg[MAX_N][MAX_N];
int ods[MAX_N], sgs[MAX_N];
void solve(){
int res = 0;
for(int k = 0; k < K; k++){
// 物品 k 存货小于需求
if(ods[k] > sgs[k]){
printf("-1\n");
return;
}
// 初始化图
int s = N + M, t = s + 1;
V = t + 1, F = ods[k];
clear_graph();
// 建图
for(int i = 0; i < N; i++) add_edge(s, i, od[i][k], 0);
for(int i = 0; i < M; i++) add_edge(N + i, t, sg[i][k], 0);
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
add_edge(i, N + j, INF, cost[k][i][j]);
}
}
res += min_cost_flow(s, t, F);
}
printf("%d\n", res);
}
int main(){
while(~scanf("%d%d%d", &N, &M, &K) && (N | M | K)){
memset(ods, 0, sizeof(ods));
memset(sgs, 0, sizeof(sgs));
for(int i = 0; i < N; i++){
for(int k = 0; k < K; k++){
scanf("%d", &od[i][k]);
ods[k] += od[i][k];
}
}
for(int i = 0; i < M; i++){
for(int k = 0; k < K; k++){
scanf("%d", &sg[i][k]);
sgs[k] += sg[i][k];
}
}
for(int k = 0; k < K; k++){
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
scanf("%d", &cost[k][i][j]);
}
}
}
solve();
}
return 0;
}