本题可以参考ccf csp-201604-4-游戏,是差不多的类型。
- 注意:对于某一地点的配餐数 以及 最终的配送距离,要用long long类型!
(1<=n<=1000, d<=n^2)
代码如下
#include<iostream>
#include<queue>
using namespace std;
const int N = 1005;
bool vis[N][N];
long long num[N][N],ans = 0;
int n;
struct node{
int x,y;
long long dis;
};
struct POS{
int x,y;
}pos[4]={{-1,0},{1,0},{0,-1},{0,1}};
queue<node> q;
void fun(){
node tmp;
while(!q.empty()){
node cur = q.front();
q.pop();
for(int i = 0; i < 4; i++){
tmp.x = cur.x+pos[i].x;
tmp.y = cur.y+pos[i].y;
if(tmp.x<1||tmp.x>n||tmp.y<1||tmp.y>n||vis[tmp.x][tmp.y]) continue;
vis[tmp.x][tmp.y] = true;
ans += num[tmp.x][tmp.y]*(cur.dis+1);
tmp.dis=cur.dis+1;
q.push(tmp);
}
}
printf("%lld\n", ans);
}
int main()
{
int m,k,d,x,y,c;
scanf("%d%d%d%d", &n, &m, &k, &d);
while(m--){
scanf("%d%d", &x, &y);
q.push({x,y,0});
}
while(k--){
scanf("%d%d%d", &x, &y, &c);
num[x][y] += c;//可能该点有多个客户,所以用叠加
}
while(d--){
scanf("%d%d", &x, &y);
vis[x][y] = true;
}
fun();
return 0;
}