66782023-12-15 22:43:16anonVillanyautócpp17Accepted 60/60731ms4272 KiB
#include <bits/stdc++.h>
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) x.begin(), x.end()
typedef long long ll;
using namespace std;
const ll INF = ~(1LL << 63);
ll N, M, K;
bool walk(ll node, ll C, vector<vector<array<ll, 2>>> &G) {
    ll i;
    unordered_set<ll> vis(N);
    vector<array<ll, 2>> best(N + 1, { -1, -1 });
    queue<array<ll, 3>> q;
    q.push({ node, K, 0 });
    while(!q.empty()) {
        array<ll, 3> cs = q.front();
        q.pop();
        vis.insert(cs[0]);
        if(vis.size() == N)
            return true;
        if(best[cs[0]][0] != -1 && (best[cs[0]][0] > cs[1] || (best[cs[0]][0] == cs[1] && best[cs[0]][1] >= cs[2])))
            continue;
        best[cs[0]][0] = cs[1];
        best[cs[0]][1] = cs[2];
        for(const auto &x : G[cs[0]]) {
            if(x[1] <= cs[2])
                q.push({ x[0], cs[1], cs[2] - x[1] });
            else if(x[1] <= C && cs[1] > 0)
                q.push({ x[0], cs[1] - 1, C - x[1] });
        }
    }
    return vis.size() == N;
}
int main() {
    FastIO;
    ll i, n1, n2, C, bsmin, bsmax;
    cin >> N >> M >> K;
    vector<vector<array<ll, 2>>> G(N + 1);
    bsmin = INF;
    bsmax = 0;
    for(i = 0; i < M; i++) {
        cin >> n1 >> n2 >> C;
        G[n1].push_back({ n2, C });
        G[n2].push_back({ n1, C });
        bsmin = min(C, bsmin);
        bsmax += C;
    }
    while(bsmin != bsmax - 1) {
        C = bsmin + (bsmax - bsmin) / 2;
        for(i = 1; i <= N; i++) {
            if(!walk(i, C, G)) {
                bsmin = C;
                goto next;
            }
        }
        bsmax = C;
        next:;
    }
    cout << bsmax << '\n';
    return 0;
}

SubtaskSumTestVerdictTimeMemory
base60/60
1Accepted0/03ms1832 KiB
2Accepted0/0731ms2540 KiB
3Accepted1/127ms2400 KiB
4Accepted1/132ms2708 KiB
5Accepted1/135ms3116 KiB
6Accepted2/2128ms2808 KiB
7Accepted2/2167ms3320 KiB
8Accepted2/2263ms3676 KiB
9Accepted1/16ms3032 KiB
10Accepted1/14ms3108 KiB
11Accepted1/14ms3184 KiB
12Accepted2/220ms3204 KiB
13Accepted2/212ms3344 KiB
14Accepted2/28ms3192 KiB
15Accepted3/312ms3412 KiB
16Accepted3/382ms3796 KiB
17Accepted2/214ms3432 KiB
18Accepted2/232ms3672 KiB
19Accepted2/235ms3688 KiB
20Accepted2/241ms3852 KiB
21Accepted2/297ms3580 KiB
22Accepted2/210ms3504 KiB
23Accepted3/364ms3508 KiB
24Accepted3/3160ms3836 KiB
25Accepted3/3167ms3992 KiB
26Accepted3/3254ms4272 KiB
27Accepted3/3558ms3824 KiB
28Accepted3/378ms3516 KiB
29Accepted3/364ms3792 KiB
30Accepted3/396ms3860 KiB