标签:
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<queue> using namespace std; int n,m,k,d; short map[1005][1005],dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; bool within(int x,int y){ if(x<=0||x>n||y<=0||y>n) return false; return true; } struct node{ int x,y,step; node(int a=0,int b=0,int c=0){ x=a; y=b; step=c; } }; queue<node> q; long long bfs(){ node n; int t=0; long long sum=0; while(!q.empty()){ n=q.front(); q.pop(); int i,x,y; for(i=0;i<4;i++){ x=n.x+dir[i][0]; y=n.y+dir[i][1]; if(within(x,y)&&!(map[x][y]&1)){ map[x][y]|=1; if(map[x][y]&2){ sum+=(map[x][y]>>2)*(n.step+1); t++; if(t==k) return sum; } q.push(node(x,y,n.step+1)); } } } } int main(){ while(scanf("%d %d %d %d",&n,&m,&k,&d)!=EOF){ int i,j,x,y; memset(map,0,sizeof(map)); for(i=0;i<m;i++){ scanf("%d %d",&x,&y); map[x][y]|=1; q.push(node(x,y,0)); } for(i=0;i<k;i++){ scanf("%d %d %d",&x,&y,&j); map[x][y]=(map[x][y]|2)+(j<<2); } for(i=0;i<d;i++){ scanf("%d %d",&x,&y); map[x][y]|=1; } printf("%I64d\n",bfs()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/13224ACMer/p/4780633.html