ZOJ--2849 Attack of Panda Virus

来源:互联网 发布:python最好的教材 编辑:程序博客网 时间:2024/06/07 02:23

在一个电脑网络中有n*m台电脑,网络中有病毒,标号为正数,负数则表示该电脑最早可以被感染病毒的天数。标号小的病毒先感染。求最后各个病毒都感染了多少台电脑。

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1849

搜索。用一个优先队列。优先队列里面存储坐标、被感染的病毒编号和第几天被感染。

优先选取先感染的点,若感染的时间一样,就选取病毒标号小的点。

#include <queue>#include <stack>#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define pb push_back#define MP make_pairtypedef long long ll;const int inf = 0x3f3f3f3f;const int mod = 1000000007;const int maxn = 510;int dxy[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };struct HeapNode{    int x, y, type, day;    HeapNode(){}    HeapNode(int _x, int _y, int _type, int _day):x(_x), y(_y), type(_type), day(_day){ }    bool operator < (const HeapNode& t) const{        return (day > t.day) || (day == t.day && type > t.type);    }};int a[maxn][maxn], vis[maxn][maxn], ans[maxn * maxn], t[maxn][maxn];int n, m, q;int main(){    while(scanf("%d%d", &n, &m) == 2){        memset(vis, 0x3f, sizeof(vis));        memset(ans, 0, sizeof(ans));        memset(t, 0x3f, sizeof(t));        priority_queue<HeapNode> Q;        while(!Q.empty()) Q.pop();        for(int i=0; i<n; i++){            for(int j=0; j<m; j++){                scanf("%d", &a[i][j]);                if(a[i][j] > 0){                    Q.push(HeapNode(i, j, a[i][j], 0));                    vis[i][j] = a[i][j];                    t[i][j] = 0;                }            }        }        while(!Q.empty()){            HeapNode now = Q.top(); Q.pop();            if(a[now.x][now.y] < 0 && (vis[now.x][now.y] < now.type || t[now.x][now.y] < now.day)) continue;//若该点已被其他标号更小的病毒感染            for(int i=0; i<4; i++){                int xx = now.x + dxy[i][0];                int yy = now.y + dxy[i][1];                if(xx < 0 || xx >= n || yy < 0 || yy >= m || a[xx][yy] > 0) continue;                int d = max(-a[xx][yy], now.day);//若被当前点感染的感染天数                if(t[xx][yy] < d) continue;                if(t[xx][yy] == d){                    if(vis[xx][yy] <= now.type) continue;                    vis[xx][yy] = now.type;                    Q.push(HeapNode(xx, yy, now.type, d));                }                else{                    t[xx][yy] = d;                    vis[xx][yy] = now.type;                    Q.push(HeapNode(xx, yy, now.type, d));                }            }        }        for(int i=0; i<n; i++)            for(int j=0; j<m; j++)            ans[ vis[i][j] ]++;        scanf("%d", &q);        int x;        while(q--){            scanf("%d", &x);            printf("%d\n", ans[x]);        }    }    return 0;}


0 0
原创粉丝点击