代码:
#include<queue>
#include<cstdio>
using namespace std;
const int MAXN = 1005;
int n,m,k,d;
int G[MAXN][MAXN];
struct position{
int x,y,cost;
position(int xx,int yy,int c=0):x(xx),y(yy),cost(c){};
};
int dir[4][2]={0,-1,0,1,-1,0,1,0};
bool visit[MAXN][MAXN];
long long ans;
int main()
{
scanf("%d %d %d %d",&n,&m,&k,&d);
int x,y;
queue<position> q;
while(m--)
{
scanf("%d %d",&x,&y);
q.push(position(x,y));
visit[x][y]=true;
}
int num;
while(k--)
{
scanf("%d %d %d",&x,&y,&num);
G[x][y]+=num;
}
while(d--)
{
scanf("%d %d",&x,&y);
G[x][y]=-1;
}
while(!q.empty())
{
position temp=q.front();
q.pop();
if(G[temp.x][temp.y]>0)
ans+=long(G[temp.x][temp.y]*temp.cost);
for(int i=0;i<4;i++)
{
x=temp.x+dir[i][0];
y=temp.y+dir[i][1];
if(x>0&&y>0&&x<=n&&y<=n&&!visit[x][y]&&G[x][y]!=-1)
{
q.push(position(x,y,temp.cost+1));
visit[x][y]=true;
}
}
}
printf("%lld",ans);
return 0;
}