23682023-01-11 19:26:57nmarciJegesmedve (50)cpp11Elfogadva 50/5010ms4536 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
const ll inf = 1e9;

ll melting[110][110];
ll dijkstra[110][110] = {0};
ll fish[110][110];
bool done[110][110] = {false};
int n, m;

vector<pair<int,int>> adj(pair<int,int> p){
  vector<pair<int,int>> v;
  for(int i = -1; i < 2; ++i){
    for(int j = -1; j < 2; ++j){
      if(abs(i + j) == 1 && p.first + i >= 0 && p.first + i < n && 
        p.second + j >= 0 && p.second + j < m){
          v.push_back({p.first + i, p.second + j});
      }
    }
  }
  return v;
}

int main()
{
  cin >> n >> m;
  pair<int,int> home;
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < m; ++j){
      cin >> melting[i][j];
      if(melting[i][j] == -1){
        home = {i, j};
      }
    }
  }
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < m; ++j){
      cin >> fish[i][j];
    }
  }
  dijkstra[home.first][home.second] = inf;
  priority_queue<pair<int,pair<int,int>>> q;
  q.push({inf, home});
  while(!q.empty()){
    auto p = q.top().second;
    q.pop();
    if(done[p.first][p.second]) continue;
    done[p.first][p.second] = true;
    vector<pair<int,int>> g = adj(p);
    for(auto i : g){
      if(dijkstra[i.first][i.second] < min(dijkstra[p.first][p.second], melting[i.first][i.second])){
        dijkstra[i.first][i.second] = min(dijkstra[p.first][p.second], melting[i.first][i.second]);
        q.push({dijkstra[i.first][i.second], i});
      }
    }
  }
  map<ll,ll> v;
  ll fish_sum = 0;
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < m; ++j){
      if(melting[i][j] == -1) continue;
      v[dijkstra[i][j]] += fish[i][j];
      fish_sum += fish[i][j];
    }
  }
  ll sol = 0;
  for(auto i : v){
    if(sol + fish_sum < i.first + 1){
      sol += fish_sum;
      break;
    }
    fish_sum -= i.second;
    sol = i.first;
  }
  sol += fish[home.first][home.second] + 1;
  cout << sol << endl;
  return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1836 KiB
2Elfogadva0/02ms2200 KiB
3Elfogadva3/33ms2468 KiB
4Elfogadva3/34ms2680 KiB
5Elfogadva3/34ms2956 KiB
6Elfogadva4/46ms3384 KiB
7Elfogadva3/32ms3104 KiB
8Elfogadva3/32ms3196 KiB
9Elfogadva3/32ms3308 KiB
10Elfogadva4/42ms3376 KiB
11Elfogadva4/44ms3780 KiB
12Elfogadva4/43ms3676 KiB
13Elfogadva4/43ms4152 KiB
14Elfogadva4/48ms4296 KiB
15Elfogadva4/49ms4380 KiB
16Elfogadva4/410ms4536 KiB