45842023-03-30 10:15:42gortomiVillanyautócpp17Time limit exceeded 49/601.064s4716 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
vector<vector<ll> > mat;
vector<pl> mini1, mini2;
vector<bool> vis;
int n, s;
bool can(ll x)
{
    ll 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 = 1e15;
    while(l + 1 != r)
    {
        ll mi = (l + r) / 2;
        if(can(mi)) r = mi;
        else l = mi;
    }
    cout << r;
}
SubtaskSumTestVerdictTimeMemory
base49/60
1Accepted0/03ms1704 KiB
2Accepted0/0552ms2036 KiB
3Accepted1/1144ms2324 KiB
4Accepted1/1141ms2564 KiB
5Accepted1/1130ms2768 KiB
6Accepted2/2959ms2792 KiB
7Accepted2/2992ms2684 KiB
8Time limit exceeded0/21.049s2904 KiB
9Accepted1/1116ms3132 KiB
10Accepted1/1115ms3408 KiB
11Accepted1/1109ms3368 KiB
12Accepted2/2783ms3756 KiB
13Accepted2/2782ms3592 KiB
14Accepted2/2768ms3848 KiB
15Accepted3/3786ms4044 KiB
16Accepted3/3921ms4236 KiB
17Accepted2/2148ms4136 KiB
18Accepted2/2165ms4076 KiB
19Accepted2/2160ms4144 KiB
20Accepted2/2157ms4144 KiB
21Accepted2/2123ms4364 KiB
22Accepted2/2143ms4148 KiB
23Accepted3/3949ms4480 KiB
24Time limit exceeded0/31.027s4672 KiB
25Time limit exceeded0/31.034s4680 KiB
26Time limit exceeded0/31.064s4536 KiB
27Accepted3/3695ms4684 KiB
28Accepted3/3955ms4712 KiB
29Accepted3/3787ms4632 KiB
30Accepted3/3731ms4716 KiB