解题思路
将所有门店入队,通过 BFS 求其到地图上各节点的最短路
求订单花费总和 $\displaystyle\sum dist[i][j] * c$
参考代码
#include <iostream>
#include <cstring>
#include <queue>
#define x first
#define y second
using namespace std;
using PII = pair<int, int>;
using LL = long long;
const int N = 1e3 + 7;
int n, m, k, d;
int g[N][N]; // 客户订单数
int dist[N][N]; // 最短路
bool vt[N][N]; // 不可访问节点
int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
int main() {
scanf("%d%d%d%d", &n, &m, &k, &d);
queue<PII> q;
memset(dist, -1, sizeof dist);
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
q.push({x, y});
dist[x][y] = 0;
}
for (int i = 0; i < k; ++i) {
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
g[x][y] += c;
}
for (int i = 0; i < d; ++i) {
int x, y;
scanf("%d%d", &x, &y);
vt[x][y] = 1;
}
while (!q.empty()) {
auto p = q.front(); q.pop();
for (int i = 0; i < 4; ++i) {
int x = p.x + dx[i], y = p.y + dy[i];
if (0 < x && x <= n && 0 < y && y <= n && !vt[x][y] && dist[x][y] == -1) {
dist[x][y] = dist[p.x][p.y] + 1;
q.push({x, y});
}
}
}
LL ans = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
ans += 1LL * g[i][j] * dist[i][j];
}
}
printf("%lld", ans);
return 0;
}