Submission #1141673

#TimeUsernameProblemLanguageResultExecution timeMemory
1141673AliMark71timeismoney (balkan11_timeismoney)C++20
50 / 100
4 ms1096 KiB
//
//  main.cpp
//  IntensiveCamp 1 2025
//
//  Created by Ali AlSalman on 24/01/2025.
//

#include <bits/stdc++.h>

template<typename T>
using vec = std::vector<T>;
using namespace std;

vec<bool> vs;
vec<vec<array<int, 3>>> adj;

pair<vec<pair<int, int>>, pair<long long, long long>> mst(int piv) {
    vec<pair<int, int>> tree; fill(vs.begin(), vs.end(), 0);
    
    long long sum_c = 0, sum_t = 0;
    auto cmp_maker = [](int i) {
        return [i](array<int, 4> lhs, array<int, 4> rhs) {
            return lhs[i] > rhs[i];
        };
    };
    
    priority_queue<
        array<int, 4>,
        vec<array<int, 4>>,
        decltype(cmp_maker(piv))
    > q(cmp_maker(piv)); q.push({0, -1, 0, 0});
    
    while (!q.empty()) {
        auto [u, v, _c, _t] = q.top(); q.pop();
        if (vs[u]) continue;
        vs[u] = 1; if (0 <= u && 0 <= v) tree.push_back({u, v});
        sum_c += _c; sum_t += _t;
        
        for (auto [c, c_c, c_t] : adj[u]) if (!vs[c]) {
            q.push({c, u, c_c, c_t});
        }
    }
    
    return {tree, {sum_c, sum_t}};
}

void solve() {
    int n, m;
    cin>>n>>m; vs.resize(n); adj.resize(n);
    if (m == n - 1) {
        pair<int, int> arr[m];
        int s = 0, t = 0;
        while (m--) {
            int a, b, c, d;
            cin>>a>>b>>c>>d; s += c; t += d;
            arr[m] = {b, a};
        }
        
        cout<<s<<" "<<t<<endl;
        for (auto [a, b] : arr) cout<<a<<" "<<b<<endl;
        exit(0);
    }
    
    while (m--) {
        int a, b, c, d;
        cin>>a>>b>>c>>d;
        adj[a].push_back({b, c, d});
        adj[b].push_back({a, c, d});
    }
    
    auto ans_c = mst(2);
    auto ans_t = mst(3);
    
    if (ans_c.second.first * ans_c.second.second <= ans_t.second.first * ans_t.second.second) {
        cout<<ans_c.second.first<<" "<<ans_c.second.second<<endl;
        for (auto [u, v] : ans_c.first) cout<<u<<" "<<v<<endl;
    } else {
        cout<<ans_t.second.first<<" "<<ans_t.second.second<<endl;
        for (auto [u, v] : ans_t.first) cout<<u<<" "<<v<<endl;
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    
    
    int t = 1;
//    cin>>t;
    while (t--) solve();
}

#Verdict Execution timeMemoryGrader output
Fetching results...