思路:普通bfs,只不过开始的点多了几个,没啥难度…
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> #include<algorithm> #include<math.h> #include<queue> using namespace std; typedef long long ll; ll a[1010][1010]= {{0}}; bool vis[1010][1010]= {{0}}; int fx[4][2]= {{1,0},{0,1},{-1,0},{0,-1}}; struct data { int x,y; ll step; data (int xx,int yy,ll ss) { x=xx,y=yy,step=ss; } }; int main() { int n, m, k, d; cin>>n>>m>>k>>d; queue<data>q; for(int i=0; i<m; i++) { int x,y; cin>>x>>y; data tt(x,y,0); q.push(tt); } for(int i=0; i<k; i++) { int x,y; ll c; cin>>x>>y>>c; a[x][y]+=c; } for(int i=0; i<d; i++) { int x,y; cin>>x>>y; vis[x][y]=1; } ll ans=0; while(!q.empty()) { data x=q.front(); q.pop(); for(int i=0; i<4; i++) { int xx=x.x+fx[i][0],yy=x.y+fx[i][1]; if(xx>0&&xx<=n&&yy>0&&yy<=n&&!vis[xx][yy]) { vis[xx][yy]=1; ans+=(x.step+1)*a[xx][yy]; data tem(xx,yy,x.step+1); q.push(tem); } } } cout<<ans<<endl; return 0; }