Submission #482787

# Submission time Handle Problem Language Result Execution time Memory
482787 2021-10-26T10:29:30 Z solarmagic timeismoney (balkan11_timeismoney) C++17
100 / 100
75 ms 776 KB
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using db = long double;
using ll = long long;

db MAX = 1e5;

struct edge{
	ll a, b, t, c;
	db k;
};
 
ll n, m;
vector<edge> v;
vector<ll> p, sz;

ll find(ll x){
	if(x == p[x]) return x;
	return p[x] = find(p[x]);
}
 
void init(ll mid) {
    fill(all(sz), 1);
    iota(all(p), 0);
    for (auto& [a, b, t, c, k] : v) {
        k = c * mid + t * (MAX - mid);
    }
	sort(all(v), [](auto& a, auto& b){return a.k < b.k;});
}

ll solve(db mid){
    init(mid);

    ll solt = 0, solc = 0;
    for (auto [a, b, t, c, k] : v) {
        a = find(a), b = find(b);
        if (a == b) continue;
        solt += t;
        solc += c;
        if(sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        p[b] = a;
	}
	return solt * solc;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

	cin >> n >> m;
	v.resize(m);
    p.resize(n);
    sz.resize(n);

    for (auto& [a, b, t, c, val] : v)
        cin >> a >> b >> t >> c;

	db l = 0, r = MAX;
	while(r - l > 1e-7) {
		db m1 = (2*l + r) / 3;
		db m2 = (l + 2*r) / 3;
		if (solve(m1) < solve(m2)) r = m2;
		else l = m1;
	}


    init(l);
    vector<pair<ll,ll>> ans;
	ll sumt = 0, sumc = 0;
    for (auto [A, B, t, c, k] : v) {
        auto a = find(A), b = find(B);
        if (a == b) continue;
        sumt += t;
        sumc += c;
        ans.emplace_back(A, B);
        if(sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        p[b] = a;
	}
    cout << sumt << ' ' << sumc << "\n";
    for (auto [a, b] : ans) {
        cout << a << ' ' << b << '\n';
    }
}

Compilation message

timeismoney.cpp: In function 'int main()':
timeismoney.cpp:57:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   57 |     for (auto& [a, b, t, c, val] : v)
      |     ^~~
timeismoney.cpp:60:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   60 |  db l = 0, r = MAX;
      |  ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 11 ms 332 KB Output is correct
8 Correct 54 ms 716 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 12 ms 404 KB Output is correct
17 Correct 12 ms 404 KB Output is correct
18 Correct 12 ms 408 KB Output is correct
19 Correct 72 ms 776 KB Output is correct
20 Correct 75 ms 776 KB Output is correct