90分代码:
//bfs
#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<int,int>PII;
const int maxn=1000+10;
bool done[maxn][maxn];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
struct node{
int x,y,dist;
node(int x,int y,int dist):x(x),y(y),dist(dist) {}
};
int n,m,k,d; //方格大小 起始点数量 终点数量 不能经过的点数量
map<PII,LL>str;
LL bfs()
{
LL sum=0;
int cnt=0; //已经经过的终点数量,作为结束标志
memset(done,false,sizeof(done));
queue<node>pq;
while(!pq.empty()) pq.pop();
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
pq.push(node(x,y,0));
done[x][y]=true;
}
str.clear();
for(int i=0;i<k;i++)
{
int x,y,weight;
scanf("%d%d%d",&x,&y,&weight);
str[PII(x,y)]+=weight;
}
for(int i=0;i<d;i++)
{
int x,y;
scanf("%d%d",&x,&y);
done[x][y]=true;
}
while(!pq.empty())
{
node f=pq.front();
pq.pop();
if(str.find(PII(f.x,f.y))!=str.end()) {
cnt++;
sum+=(LL)f.dist*str[PII(f.x,f.y)];
}
if(cnt==k) return sum;
for(int i=0;i<4;i++)
{
int nx=f.x+dx[i],ny=f.y+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&!done[nx][ny])
{
pq.push(node(nx,ny,f.dist+1));
done[nx][ny]=true;
}
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&d);
cout<<bfs()<<endl;
return 0;
}