具体代码:
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 1005;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k, d, x, y, c, ans, dis[maxn][maxn];
bool vis[maxn][maxn];
struct node
{
int x, y, c;
} a[maxn * maxn];
queue<pair<int, int>> q;
bool check(int x, int y)
{
if (x >= 1 && x <= n && y >= 1 && y <= n && vis[x][y] == false)
return true;
return false;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> k >> d;
for (int i = 1; i <= m; ++i) // 输入分店的位置
{
cin >> x >> y;
q.push(make_pair(x, y)); // 将每个分店都加入队列中,同时进行BFS
vis[x][y] = true;
dis[x][y] = 0;
}
for (int i = 1; i <= k; ++i) // 输入客户的位置
{
cin >> a[i].x >> a[i].y >> a[i].c;
}
for (int i = 1; i <= d; ++i) // 输入不能经过的点
{
cin >> x >> y;
vis[x][y] = 1;
}
while (!q.empty())
{
pair<int, int> p = q.front();
q.pop();
for (int i = 0; i < 4; ++i)
{
if (check(p.first + dx[i], p.second + dy[i]))
{
vis[p.first + dx[i]][p.second + dy[i]] = true;
dis[p.first + dx[i]][p.second + dy[i]] = dis[p.first][p.second] + 1;
q.push(make_pair(p.first + dx[i], p.second + dy[i]));
}
}
}
for (int i = 1; i <= k; ++i)
{
ans += dis[a[i].x][a[i].y] * a[i].c;
}
cout << ans << endl;
system("pause");
return 0;
}