103602024-04-01 03:11:56VargusRácsháló gráfcpp17Accepted 50/50130ms4204 KiB
#include <iostream>
#include <queue>
#include <algorithm>
#include <climits>
#define ll long long

using namespace std;

struct adat
{
    ll hossz = INT_MAX;
    vector < pair <ll, ll> > sz;
};
vector <adat> x;

struct tav
{
    ll hova, kolt;
};
priority_queue <tav> t;

bool operator < (const tav& a, const tav& b)
{
    return a.kolt > b.kolt;
}

vector <ll> maxi;

ll n, m;
void dijkstra(ll kezd)
{
    tav akt;
    t.push({ kezd, 0 });
    x[kezd].hossz = 0;
    while (!t.empty())
    {
        akt = t.top();
        t.pop();
        for (auto& e : x[akt.hova].sz)
        {
            if (x[e.first].hossz > (e.second + akt.kolt))
            {
                x[e.first].hossz = e.second + akt.kolt;
                t.push({ e.first, x[e.first].hossz });
            }
        }
    }
    ll maximum = INT_MIN;
    for (ll i = 1; i <= n * m; ++i)
        if (x[i].hossz > maximum)
            maximum = x[i].hossz;

    maxi.push_back(maximum);
}

int main()
{
    ll k;
    cin >> n >> m >> k;
    x.resize(n * m + 1);
    for (ll i = 1; i <= n * m; ++i)
    {
        if (i > m)
        {
            x[i].sz.push_back({ i - m, 1 });
            x[i - m].sz.push_back({ i, 1 });
        }
        if (i % m == 0)
            continue;
        x[i].sz.push_back({ i + 1, 1 });
        x[i + 1].sz.push_back({ i, 1 });
    }
    for (ll i = 1; i <= k; ++i)
    {
        ll a, b;
        cin >> a >> b;
        x[a].sz.push_back({ b, 1 });
        x[b].sz.push_back({ a, 1 });
        for (ll j = 1; j <= n * m; ++j)
        {
            dijkstra(j);
            for (ll g = 1; g <= n * m; ++g)
            {
                x[g].hossz = INT_MAX;
            }
        }
        ll nagy = INT_MIN;
        for (auto& e : maxi)
            if (nagy < e)
                nagy = e;
        maxi.clear();
        
        cout << nagy << endl;
    }

    return 0;
}
/*
3 4 4
1 6
7 10
2 12
4 9
*/
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1872 KiB
2Accepted0/0128ms2232 KiB
3Accepted2/23ms2540 KiB
4Accepted2/23ms2420 KiB
5Accepted2/23ms2520 KiB
6Accepted2/24ms2732 KiB
7Accepted2/28ms2976 KiB
8Accepted2/28ms3064 KiB
9Accepted2/28ms3176 KiB
10Accepted2/24ms3280 KiB
11Accepted2/28ms3352 KiB
12Accepted2/227ms3452 KiB
13Accepted3/359ms3544 KiB
14Accepted3/39ms3672 KiB
15Accepted3/357ms3764 KiB
16Accepted3/38ms3760 KiB
17Accepted3/348ms3684 KiB
18Accepted3/318ms3900 KiB
19Accepted3/33ms3900 KiB
20Accepted3/34ms3892 KiB
21Accepted3/321ms3904 KiB
22Accepted3/3130ms4204 KiB