题解:P13383 [GCJ 2011 Finals] Rains Over Atlantis
whyc_s_pT2 · · 题解
注意力惊人…?
我们不妨就求出每一个点变成
对于一个点,我们分析它被侵蚀的过程,如果存在一个点的水位与它相差超过
这个的求法需要先观察到边界上的点已经确定,而转移与图论的最短路是类似的,由于对于转移取最小,dijkstra 算法是成立的,只是点权有下界。
code:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 25;
int n,m;ll a[N][N],w,dis[N][N];
struct dot{
int x,y;ll z;
bool operator<(const dot &b)const{return z>b.z;}
};
int dx[] = {0,0,1,-1},dy[] = {1,-1,0,0};
bool check(int x,int y){
if(x<1 || y<1 || x>n || y>m)return false;
return true;
}
bool vis[N][N];
void solve(int id){
cin >> n >> m >> w;
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue<dot> q;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j){
cin >> a[i][j];
if(i == 1|| j == 1 || i==n || j==m){
q.push({i,j,(a[i][j]+w-1)/w});
dis[i][j] = (a[i][j]+w-1)/w;
}
}
while(!q.empty()){
dot now = q.top();q.pop();
int x = now.x,y=now.y;
if(vis[x][y])continue;
vis[x][y] = 1;
for(int i=0;i<4;++i){
int nx = x + dx[i],ny = y + dy[i];
if(check(nx,ny)){
ll val = max((a[nx][ny]+w-1)/w,dis[x][y]+(a[nx][ny] > 0));
if(dis[nx][ny] > val){
dis[nx][ny] = val;
q.push({nx,ny,dis[nx][ny]});
}
}
}
}
ll ans = 0;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
ans = max(ans,dis[i][j]);
cout<<"Case #"<<id<<": "<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int _,c=0;cin >> _;
while(_--){
solve(++c);
}
return 0;
}