https://loj.ac/problem/6224
对于每一条路径,建两条边,一条流量为1,费用为权值;一条流量为 +∞ + ∞ ,费用为0,跑MCMF即可。
/************************************************
* Au: Hany01
* Date: Jul 13th, 2018
* Prob: LOJ6224 深海机器人
* Email: [email protected]
* Inst: Yali High School
************************************************/
#include
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 17 * 17, maxm = maxn << 4;
int P, Q, a, b, beg[maxn], nex[maxm], v[maxm], w[maxm], f[maxm], S, T, vis[maxn], dis[maxn], e = 1, cost;
inline void add(int uu, int vv, int ff, int ww, int fl = 1) {
v[++ e] = vv, f[e] = ff, w[e] = ww, nex[e] = beg[uu], beg[uu] = e;
if (fl) add(vv, uu, 0, -ww, 0);
}
#define idx(i, j) ((i) * (Q + 1) + (j + 1))
inline bool BFS()
{
static queue<int> q;
For(i, 1, T) dis[i] = INF;
Set(vis, 0), dis[S] = 0, q.push(S);
while (!q.empty()) {
int u = q.front(); q.pop(), vis[u] = 0;
for (register int i = beg[u]; i; i = nex[i])
if (f[i] && chkmin(dis[v[i]], dis[u] + w[i]))
if (!vis[v[i]]) vis[v[i]] = 1, q.push(v[i]);
}
return dis[T] != INF;
}
int DFS(int u, int flow)
{
if (u == T) return flow;
int t, res = flow;
vis[u] = 1;
for (register int i = beg[u]; i; i = nex[i])
if (f[i] && dis[v[i]] == dis[u] + w[i] && !vis[v[i]]) {
f[i] -= (t = DFS(v[i], min(res, f[i]))), f[i ^ 1] += t, cost += t * w[i];
if (!(res -= t)) return flow;
}
return flow - res;
}
int main()
{
#ifdef hany01
File("loj6224");
#endif
a = read(), b = read(), P = read(), Q = read();
For(i, 0, P) rep(j, Q) add(idx(i, j), idx(i, j + 1), 1, -read()), add(idx(i, j), idx(i, j + 1), INF, 0);
For(j, 0, Q) rep(i, P) add(idx(i, j), idx(i + 1, j), 1, -read()), add(idx(i, j), idx(i + 1, j), INF, 0);
T = (S = (P + 1) * (Q + 1) + 1) + 1;
while (a --) {
register int tot = read(), x = read(), y = read();
add(S, idx(x, y), tot, 0);
}
while (b --) {
register int tot = read(), x = read(), y = read();
add(idx(x, y), T, tot, 0);
}
while (BFS()) do Set(vis, 0), DFS(S, INF); while (vis[T]);
printf("%d\n", -cost);
return 0;
}
//莫笑贱贫夸富贵,共成枯骨两何如?
// -- 白居易《放言五首·其四》