45882023-03-30 10:20:43gortomiVillanyautócpp17Accepted 60/60797ms5216 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<int, ll>;
vector<vector<ll> > mat;
vector<pl> mini1, mini2;
vector<bool> vis;
int n, s;
bool can(ll x)
{
    int maxi = 0;
    for(int i = 1; i <= n; i++)
    {
        mini1.assign(n + 1, {1e9, 1e18});
        mini2.assign(n + 1, {1e9, 1e18});
        vis.assign(2 * n + 1, 0);
        mini1[i] = {0, x};
        for(int j = 0; j < 2 * n; j++)
        {
            int act = -1;
            pl mv = {1e9 + 1, 1e18 + 1};
            for(int k = 1; k <= n; k++)
            {
                if(mini1[k] < mv && !vis[k])
                {
                    mv = mini1[k];
                    act = k;
                }
                if(mini2[k] < mv && !vis[k + n])
                {
                    mv = mini2[k];
                    act = k + n;
                }
            }
            vis[act] = 1;
            if(act <= n)
            {
                for(int k = 1; k <= n; k++)
                {
                    if(mat[act][k] > x) continue;
                    ll a = mini1[act].first, b = mini1[act].second;
                    pl len = mini1[act];
                    if(x - b < mat[act][k])
                    {
                        len.first++;
                        len.second = mat[act][k];
                    }
                    else len.second += mat[act][k];
                    if(len < mini1[k])
                    {
                        mini1[k] = len;
                    }
                    if(len.first != mini1[k].first) mini2[k] = min(mini2[k], len);
                }
            }
            else
            {
                act -= n;
                for(int k = 1; k <= n; k++)
                {
                    if(mat[act][k] > x) continue;
                    ll a = mini2[act].first, b = mini2[act].second;
                    pl len = mini2[act];
                    if(x - b < mat[act][k])
                    {
                        len.first++;
                        len.second = mat[act][k];
                    }
                    else len.second += mat[act][k];
                    if(len < mini1[k])
                    {
                        mini1[k] = len;
                    }
                    if(len.first != mini1[k].first) mini2[k] = min(mini2[k], len);
                }
            }
        }
        for(int j = 1; j <= n; j++) maxi = max(maxi, mini1[j].first);
    }
    return maxi <= s;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int m;
    cin >> n >> m >> s;
    mat.assign(n + 1, vector<ll>(n + 1, 1e18));
    for(int i = 0; i < m; i++)
    {
        ll x, y, z;
        cin >> x >> y >> z;
        mat[x][y] = z;
        mat[y][x] = z;
    }
    long long l = 0, r = 1e12;
    while(l + 1 != r)
    {
        ll mi = (l + r) / 2;
        if(can(mi)) r = mi;
        else l = mi;
    }
    cout << r;
}
SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/03ms1832 KiB
2Accepted0/0419ms2236 KiB
3Accepted1/1112ms2384 KiB
4Accepted1/196ms2600 KiB
5Accepted1/192ms2812 KiB
6Accepted2/2722ms3088 KiB
7Accepted2/2654ms3244 KiB
8Accepted2/2680ms3512 KiB
9Accepted1/187ms3448 KiB
10Accepted1/182ms3716 KiB
11Accepted1/176ms3928 KiB
12Accepted2/2540ms4136 KiB
13Accepted2/2524ms4096 KiB
14Accepted2/2515ms4352 KiB
15Accepted3/3523ms4312 KiB
16Accepted3/3652ms4336 KiB
17Accepted2/2114ms4252 KiB
18Accepted2/2122ms4264 KiB
19Accepted2/2111ms4320 KiB
20Accepted2/2114ms4508 KiB
21Accepted2/297ms4456 KiB
22Accepted2/2111ms4528 KiB
23Accepted3/3695ms4852 KiB
24Accepted3/3727ms4816 KiB
25Accepted3/3712ms4940 KiB
26Accepted3/3797ms4956 KiB
27Accepted3/3497ms5216 KiB
28Accepted3/3699ms5192 KiB
29Accepted3/3570ms4988 KiB
30Accepted3/3533ms5008 KiB