P4012 深海機器人問題
\(\color{#0066ff}{題目描述}\)
深海資源考察探險隊的潛艇將到達深海的海底進行科學考察。
潛艇內有多個深海機器人。潛艇到達深海海底後,深海機器人將離開潛艇向預定目標移動。
深海機器人在移動中還必須沿途採集海底生物標本。沿途生物標本由最先遇到它的深海機器人完成採集。
每條預定路徑上的生物標本的價值是已知的,而且生物標本只能被採集一次。
本題限定深海機器人只能從其出發位置沿著向北或向東的方向移動,而且多個深海機器人可以在同一時間佔據同一位置。
用一個 \(P\times Q\) 網格表示深海機器人的可移動位置。西南角的座標為 \((0,0)\),東北角的座標為 \((Q,P)\)
給定每個深海機器人的出發位置和目標位置,以及每條網格邊上生物標本的價值。
計算深海機器人的最優移動方案, 使深海機器人到達目的地後,採集到的生物標本的總價值最高。
\(\color{#0066ff}{輸入格式}\)
檔案的第 \(1\) 行為深海機器人的出發位置數 \(a\),和目的地數 \(b\) 。
第 \(2\) 行為 \(P\) 和 \(Q\) 的值。
接下來的 \(P+1\) 行,每行有 \(Q\) 個正整數,表示向東移動路徑上生物標本的價值,行資料依從南到北方向排列。
再接下來的 \(Q+1\) 行,每行有 \(P\) 個正整數,表示向北移動路徑上生物標本的價值,行資料依從西到東方向排列。
接下來的 \(a\) 行,每行有 \(3\) 個正整數 \(k,x,y\),表示有 \(k\) 個深海機器人從 \((x,y)\) 位置座標出發。
再接下來的 \(b\) 行,每行有 \(3\) 個正整數 \(r,x,y\) ,表示有 \(r\) 個深海機器人可選擇 \((x,y)\) 位置座標作為目的地。
\(\color{#0066ff}{輸出格式}\)
輸出採集到的生物標本的最高總價值.
\(\color{#0066ff}{輸入樣例}\)
1 1
2 2
1 2
3 4
5 6
7 2
8 10
9 3
2 0 0
2 2 2
\(\color{#0066ff}{輸出樣例}\)
42
\(\color{#0066ff}{資料範圍與提示}\)
\(1\leq P,Q\leq 15\)
\(1\leq a\leq 4\)
\(1\leq b\leq 6\)
\(\color{#0066ff}{題解}\)
S向所有機器人起點連邊
所有機器人終點向T連邊
每個點向右向下分別連兩條邊
一條容量為 1 ,邊權為圖中邊權
另一條容量為 inf,邊權為零
這樣保證了權值只能獲得一次,又保證了這條邊可以被多個人走
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define _ 0
#define LL long long
#define int long long
inline LL in() {
LL x = 0, f = 1; char ch;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
return x * f;
}
const int maxn = 105050;
struct node {
int to, dis, can;
node *nxt, *rev;
node(int to = 0, int dis = 0, int can = 0, node *nxt = NULL):to(to), dis(dis), can(can), nxt(nxt) {}
void *operator new (size_t) {
static node *S = NULL, *T = NULL;
return (S == T) && (T = (S = new node[1024]) + 1024), S++;
}
};
const int inf = 0x7fffffff;
int n, m, p, q, s, t;
typedef node* nod;
bool vis[maxn];
nod head[maxn], road[maxn];
int dis[maxn], change[maxn], id[55][55];
std::queue<int> V;
inline void add(int from, int to, int can, int dis) {
nod o = new node(to, dis, can, head[from]);
head[from] = o;
}
inline void link(int from, int to, int can, int dis) {
add(from, to, can, dis);
add(to, from, 0, -dis);
head[from]->rev = head[to];
head[to]->rev = head[from];
}
inline bool spfa()
{
for(int i = s; i <= t; i++) dis[i] = -inf, change[i] = inf;
V.push(s);
dis[s] = 0;
while(!V.empty()) {
int tp = V.front(); V.pop();
vis[tp] = false;
for(nod i = head[tp]; i; i = i->nxt) {
if(dis[i->to] < dis[tp] + i->dis && i->can > 0) {
dis[i->to] = dis[tp] + i->dis;
road[i->to] = i;
change[i->to] = std::min(change[tp], i->can);
if(!vis[i->to]) vis[i->to] = true, V.push(i->to);
}
}
}
return change[t] != inf;
}
inline void mcmf() {
int cost = 0;
while(spfa()) {
cost += dis[t] * change[t];
for(int o = t; o != s; o = road[o]->rev->to) {
road[o]->can -= change[t];
road[o]->rev->can += change[t];
}
}
printf("%lld", cost);
}
signed main() {
n = in(), m = in(), p = in(), q = in();
for(int cnt = 0, i = 0; i <= p; i++)
for(int j = 0; j <= q; j++)
id[i][j] = ++cnt;
s = 0, t = id[p][q] + 1;
for(int i = 0; i <= p; i++)
for(int j = 0; j < q; j++)
link(id[i][j], id[i][j+1], 1, in());
for(int i = 0; i <= q; i++)
for(int j = 0; j < p; j++)
link(id[j][i], id[j+1][i], 1, in());
int k, x, y;
for(int i = 1; i <= n; i++) {
k = in(), x = in(), y = in();
link(s, id[x][y], k, 0);
}
for(int i = 1; i <= m; i++) {
k = in(), x = in(), y = in();
link(id[x][y], t, k, 0);
}
for(int i = 0; i <= p; i++)
for(int j = 0; j <= q; j++) {
if(i + 1 <= p) link(id[i][j], id[i+1][j], inf, 0);
if(j + 1 <= q) link(id[i][j], id[i][j+1], inf, 0);
}
mcmf();
return 0 ;
}