标签:
最优配餐
时间限制: | 1.0s |
内存限制: | 256.0MB |
1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 #define LL long long 4 using namespace std; 5 const int maxn = 1010; 6 const int dir[4][2] = {-1,0,0,-1,1,0,0,1}; 7 int n,m,k,d,e[maxn][maxn] = {0}; 8 struct node { 9 int x,y,step; 10 node(int a = 0,int b = 0,int c = 0) { 11 x = a; 12 y = b; 13 step = c; 14 } 15 }; 16 queue<node>q; 17 bool isIn(int x,int y) { 18 return x > 0 && x <= n && y > 0 && y <= n; 19 } 20 LL bfs() { 21 LL ans = 0; 22 int cnt = 0; 23 while(!q.empty()) { 24 node now = q.front(); 25 q.pop(); 26 for(int i = 0; i < 4; ++i) { 27 int nx = now.x+ dir[i][0]; 28 int ny = now.y + dir[i][1]; 29 if(isIn(nx,ny)&&!(e[nx][ny]&1)) { 30 e[nx][ny] |= 1; 31 q.push(node(nx,ny,now.step+1)); 32 if(e[nx][ny]&2) { 33 ans += (now.step+1)*(e[nx][ny]>>2); 34 if(++cnt == k) return ans; 35 } 36 } 37 } 38 } 39 return ans; 40 } 41 int main() { 42 int x,y,z; 43 while(~scanf("%d %d %d %d",&n,&m,&k,&d)) { 44 for(int i = 0; i < m; ++i) { 45 scanf("%d %d",&y,&x); 46 q.push(node(x,y,0)); 47 e[x][y] |= 1; 48 } 49 for(int i = 0; i < k; ++i) { 50 scanf("%d %d %d",&y,&x,&z); 51 e[x][y] = (e[x][y]|2) + (z<<2); 52 } 53 for(int i = 0; i < d; ++i) { 54 scanf("%d %d",&y,&x); 55 e[x][y] |= 1; 56 } 57 printf("%I64d\n",bfs()); 58 } 59 return 0; 60 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4356069.html