62852023-11-13 12:02:05hackemonJegesmedve (50)cpp17Elfogadva 50/5020ms28720 KiB
#include <bits/stdc++.h>
using namespace std;

int n = 1000, m = 1000;

vector<vector<int>> olvpont(n + 1, vector<int>(m + 1)), hal(n + 1, vector<int>(m + 1));
vector<vector<int>> legrovidebb_ut(n + 1, vector<int>(m + 1, -1)); // int max a legrovidebb ut, ha meg nem latogattuk meg
set<pair<int, pair<int, int>>> most;

void kov_elem(int i, int j, int p)
{
    if (i <= n && j <= m && i >= 1 && j >= 1) // hogyha benne van a racsban
    {
        if (legrovidebb_ut[i][j] < min(olvpont[i][j], p))
        {
            // If we find a shorter path to the point, update it and insert into 'most'
            most.erase({-legrovidebb_ut[i][j], {i, j}});
            legrovidebb_ut[i][j] = min(olvpont[i][j], p);
            most.insert({-legrovidebb_ut[i][j], {i, j}});
        }
    }
}

int main()
{
    cin >> n >> m;

    int x, y;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> olvpont[i][j];
            if (olvpont[i][j] == -1)
            {
                x = i;
                y = j;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> hal[i][j];
        }
    }

    legrovidebb_ut[x][y] = INT_MAX;
    most.insert({-INT_MAX, {x, y}});

    // modified dijkstra algorithm

    while (!most.empty())
    {
        // eloszor hozzaadja az elso pontot
        auto ertek = *most.begin(); 
        ertek.first *= -1; // minuszba volt ezert pozitivba at kell vinni
        auto hely = ertek.second; // ez az index helye
        most.erase(most.begin()); // kivesszuk a latottak listajabol
        kov_elem(hely.first + 1, hely.second, ertek.first);
        kov_elem(hely.first, hely.second + 1, ertek.first);
        kov_elem(hely.first - 1, hely.second, ertek.first);
        kov_elem(hely.first, hely.second - 1, ertek.first);
        
    }
    int ans = 0; 

    vector<pair<int, int>> vegso;
    for(int i = 1;i<=n;i++ ) {
        for(int j = 1;j<=m;j++ ) {
            vegso.push_back({legrovidebb_ut[i][j], hal[i][j]});
        }
    }
    sort(vegso.begin(), vegso.end());
    
    for(pair<int, int> i : vegso) {
        ans = min(ans + i.second, i.first);
    }
    cout << ans + 1 << endl;
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/013ms25568 KiB
2Elfogadva0/010ms25848 KiB
3Elfogadva3/313ms26128 KiB
4Elfogadva3/314ms26384 KiB
5Elfogadva3/314ms26384 KiB
6Elfogadva4/418ms26864 KiB
7Elfogadva3/312ms26416 KiB
8Elfogadva3/312ms26564 KiB
9Elfogadva3/310ms26768 KiB
10Elfogadva4/49ms26980 KiB
11Elfogadva4/412ms27404 KiB
12Elfogadva4/414ms27212 KiB
13Elfogadva4/413ms27224 KiB
14Elfogadva4/418ms27768 KiB
15Elfogadva4/419ms28200 KiB
16Elfogadva4/420ms28720 KiB