1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <iostream> #include <cstring> #include <algorithm> #include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
bool st[N][N]; int dist[N][N]; bool g[N][N]; PII start[N * N];
struct { int x, y, num; } order[N * N];
int n, m, k, d;
int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1};
long long bfs() { queue<PII> q; memset(dist, 0x3f, sizeof dist); for (int i = 1; i <= m; i++) { int x = start[i].first, y = start[i].second; dist[x][y] = 0; q.push({x, y}); }
while (q.size()) { auto t = q.front(); q.pop(); int x = t.first, y = t.second; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 1 || nx > n || ny < 1 || ny > n) continue; if (g[nx][ny] || st[nx][ny]) continue; if (dist[nx][ny] > dist[x][y] + 1) { dist[nx][ny] = dist[x][y] + 1; q.push({nx, ny}); } } } long long res = 0; for (int i = 1; i <= k; i ++) { int x = order[i].x, y = order[i].y; res += (long long)dist[x][y] * order[i].num; } return res; }
int main() { cin >> n >> m >> k >> d; for (int i = 1; i <= m; i++) { scanf("%d%d", &start[i].first, &start[i].second); }
for (int i = 1; i <= k; i++) { int x, y, c; scanf("%d%d%d", &x, &y, &c); order[i] = {x, y, c}; }
for (int i = 1; i <= d; i++) { int x, y; scanf("%d%d", &x, &y); g[x][y] = true; }
cout << bfs() << endl;
return 0; }
|