1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define ROF(i, a, b) for (int i = a; i >= b; --i) #define all(x,size) x.begin()+1,x.begin()+size+1 #define all1(x) x.begin(),x.end()
using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; typedef array<ll,3> arr; typedef double DB; const ll MAXN=510,MAXval=static_cast<ll>(1e18)+3;
ll N,M,X,sx,sy; ll maze[MAXN][MAXN]; bool vis[MAXN][MAXN]; struct cmp{ bool operator()(pll a,pll b){ return maze[a.first][a.second]>maze[b.first][b.second]; } }; ll dx[4]={1,0,-1,0}; ll dy[4]={0,1,0,-1};
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>N>>M>>X>>sx>>sy; FOR(i, 1, N){ FOR(j, 1, M){ cin>>maze[i][j]; } } priority_queue<pll,vector<pll>,cmp> pq; pq.push({sx,sy}); vis[sx][sy]=true; ll ans=0;
while (!pq.empty()) { ll x=pq.top().first,y=pq.top().second; pq.pop(); if(maze[x][y]*1.0l <ans*1.0l/X || (x==sx && y==sy)){ ans+=maze[x][y];
FOR(i, 0, 3){ ll fx=x+dx[i],fy=y+dy[i]; if(fx<1 || fx>N) continue; if(fy<1 || fy>M) continue; if(vis[fx][fy]) continue; vis[fx][fy]=true; pq.push({fx,fy}); } } } cout<<ans<<'\n'; return 0; }
|