237982026-01-29 19:24:56abcdVillanyautócpp17Wrong answer 52/6097ms556 KiB
#include <bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using ll=long long;

const int maxn=101;
vector<pii> adj[maxn];
int k, n;
pii a[maxn];
queue<int> q;
bool inq[maxn];
ll ans=-1;

bool bfs(int i, int mx){
    for (int j=1;j<=n;j++) {
        a[j]={-1,-1};
    }
    a[i]={1, mx};
    q.push(i);
    inq[i]=true;
    while (q.size()) {
        int v = q.front();
        q.pop();
        inq[v]=false;
        for (auto to:adj[v]){
            if (to.second >mx) continue;
            int to1 = to.first;
            int b1 = a[v].first, b2=a[v].second;
            if (b2 < to.second) {
                b1++;
                b2=mx-to.second;
            } else b2-=to.second;
            pii prev=a[to1];
            if (a[to1].first != -1 && a[to1].first > b1) a[to1]={b1, b2};
            else if(a[to1].first==b1&&a[to1].second < b2) a[to1]={b1, b2};
            else if(a[to1].first==-1)a[to1]={b1, b2};
            if (prev!=a[to1] && inq[to1]==false) {q.push(to1);inq[to1]=true;}
        }
    }
    for (int j=1;j<=n;j++) {
        auto& p=a[j];
        if (p.first > k || p.first==-1) {
            return false;
        }
    }
    return true;
}

int main() {
    int m;
    cin>>n>>m>>k;
    for (int i=0;i<m;i++) {
        int a, b, c; cin>>a>>b>>c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
    }
    ll l = 1,r=1e9;
    while (l<r){
        ll mid=l + (r - l) / 2;
        bool ok=true;
        for(int i = 1;i<=n;i++) {
            if (!bfs(i, mid)) {ok=false; break;}
        }
        if (ok) {
            r=mid;
            ans=mid;
        } else{
            l=mid+1;
        }
    }
    cout << ans << '\n';
}

/*
4 4 2   
1 2 1
2 3 2
3 4 3
4 1 4
*/
SubtaskSumTestVerdictTimeMemory
base52/60
1Accepted0/01ms316 KiB
2Accepted0/097ms472 KiB
3Wrong answer0/11ms316 KiB
4Accepted1/112ms316 KiB
5Accepted1/113ms456 KiB
6Wrong answer0/22ms316 KiB
7Accepted2/257ms508 KiB
8Accepted2/293ms500 KiB
9Accepted1/14ms316 KiB
10Accepted1/13ms316 KiB
11Accepted1/13ms316 KiB
12Accepted2/217ms412 KiB
13Accepted2/29ms316 KiB
14Accepted2/28ms412 KiB
15Accepted3/312ms412 KiB
16Accepted3/332ms448 KiB
17Wrong answer0/21ms344 KiB
18Accepted2/213ms424 KiB
19Accepted2/213ms316 KiB
20Accepted2/217ms456 KiB
21Accepted2/214ms424 KiB
22Accepted2/24ms316 KiB
23Wrong answer0/31ms316 KiB
24Accepted3/359ms440 KiB
25Accepted3/354ms508 KiB
26Accepted3/393ms556 KiB
27Accepted3/372ms448 KiB
28Accepted3/320ms408 KiB
29Accepted3/310ms316 KiB
30Accepted3/318ms412 KiB