poj 2227 The Wedding Juicer(优先队列+bfs)

题目大意】:给定n*m地图,其中高度不同,问最多能存多少水(和木桶一样,最外层不能存水)。


【解题思路】:每次利用优先队列找出最小的进行扩展。Orz...把它的行列搞反了,看了n久才过的sample~...-_-!!!

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

struct Node {
    ll h;
    int x,y;
    Node(){}
    Node(ll a,int b,int c) {
        h=a,x=b,y=c;
    }
    bool operator <(const Node &a) const {
        return h>a.h;
    }
};

int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int n,m;
ll mapp[500][500];
int vis[500][500];
ll ans;

bool check(int x, int y) {
    if (x>0 && x<=n && y>0 && y<=m) return true;
    return false;
}

void solve_pq(){
    priority_queue<Node> que;
    memset(vis,0,sizeof(vis));
    for (int i=1; i<=m; i++){
        que.push(Node(mapp[1][i],1,i));        
        que.push(Node(mapp[n][i],n,i));
        vis[1][i]=1;
        vis[n][i]=1;
    }
    for (int i=2; i<n; i++){
        que.push(Node(mapp[i][1],i,1));        
        que.push(Node(mapp[i][m],i,m));
        vis[i][1]=1;
        vis[i][m]=1;
    }
    while(!que.empty()){
        Node tmp=que.top();
        que.pop();
        for (int i=0; i<4; i++){
            int tx,ty;
            tx=tmp.x+dx[i];
            ty=tmp.y+dy[i];
            if (vis[tx][ty]==0 && check(tx,ty)){
				if (mapp[tx][ty]<tmp.h){
                    ans=ans+tmp.h-mapp[tx][ty];
					que.push(Node(tmp.h,tx,ty));
                } 
                else que.push(Node(mapp[tx][ty],tx,ty));
                vis[tx][ty]=1;
            }
        }
    }
    return ;
}

int main() {
    while (~scanf("%d%d",&m,&n)){
        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++)
                scanf("%lld",&mapp[i][j]);
        ans=0;
        solve_pq();
        printf("%lld\n", ans);
    }
    return 0;
}


你可能感兴趣的:(poj 2227 The Wedding Juicer(优先队列+bfs))