Submission

Status:

[PPPPPP-SSS]

Subtask/Task Score:

{0/100}

Score: 0

User: KantaponZ

Problemset: รัฐบาล

Language: cpp

Time: 0.050 second

Submitted On: 2025-08-24 15:16:19

#include <bits/stdc++.h>
using namespace std;

#define ll long long

int N, M;
vector<tuple<int,int,int>> Edge;
vector<tuple<int,int,int>> Used;
priority_queue<tuple<int,int,int>> pq;
int pa[105];
int ans1, ans2 = 1e9;

int fp(int n) {
    if (pa[n] == n) return n;
    return pa[n] = fp(pa[n]);
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> N >> M;
    for (int i = 0; i < M; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        Edge.emplace_back(u, v, w);
        pq.emplace(-w, u, v);
    }

    // find the shortest cost
    for (int i = 1; i <= N; i++) pa[i] = i;
    while (!pq.empty()) {
        auto [w, u, v] = pq.top();
        pq.pop(); w *= -1;
        int pu = fp(u), pv = fp(v);
        if (pu == pv) continue;
        pa[pu] = pv;
        ans1 += w;
        Used.emplace_back(u, v, w);
    }

    //find the second
    for (int i = 0; i < Used.size(); i++) {
        auto [u1, v1, w1] = Used[i];
        for (int j = 0; j < Edge.size(); j++) {
            auto [u2, v2, w2] = Edge[j];
            if ((w1 == w2) && (((u1 == v2) && (v1 == u2)) || ((u1 == u2) && (v1 == v2)))) continue;
            pq.emplace(-w2, u2, v2);
        }
        int temp = 0;
        int ct = 0;
        for (int i = 1; i <= N; i++) pa[i] = i;
        while (!pq.empty()) {
            auto [w, u, v] = pq.top();
            pq.pop(); w *= -1;
            int pu = fp(u), pv = fp(v);
            if (pu == pv) continue;
            pa[pu] = pv;
            temp += w;
            ct++;
        }
        if (temp <= ans1 || ct != N - 1) continue;
        ans2 = min(ans2, temp);
    }

    cout << ans1 << " " << ans2;

}