Submission #741732

# Submission time Handle Problem Language Result Execution time Memory
741732 2023-05-14T17:44:47 Z abcvuitunggio timeismoney (balkan11_timeismoney) C++17
100 / 100
442 ms 644 KB
#include <bits/stdc++.h>
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
using namespace std;
struct edge{
    int u,v,t,c;
}e[10001];
int n,m,p[201],sz[201],mn=1e18;
vector <edge> v,res;
pii q;
int f(int i){
    return (p[i]==i?i:p[i]=f(p[i]));
}
int unite(int i, int j){
    i=f(i);
    j=f(j);
    if (i==j)
        return 0;
    if (sz[i]<sz[j])
        swap(i,j);
    p[j]=i;
    sz[i]+=sz[j];
    return 1;
}
int A,B;
pii calc(int a, int b){
    int t=0,c=0;
    A=a,B=b;
    for (int i=0;i<n;i++){
        p[i]=i;
        sz[i]=1;
    }
    sort(e+1,e+m+1,[](edge x, edge y){return x.t*A+x.c*B<y.t*A+y.c*B;});
    v.clear();
    for (int i=1;i<=m;i++)
        if (unite(e[i].u,e[i].v)){
            v.push_back(e[i]);
            t+=e[i].t;
            c+=e[i].c;
        }
    if (mn>t*c){
        mn=t*c;
        q={t,c};
        res=v;
    }
    return {t,c};
}
void g(pii a, pii b){
    auto c=calc(a.se-b.se,b.fi-a.fi);
    if ((c.se-a.se)*(b.fi-a.fi)==(b.se-a.se)*(c.fi-a.fi))
        return;
    g(a,c);
    g(c,b);
}
int32_t main(){
    ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
    cin >> n >> m;
    for (int i=1;i<=m;i++)
        cin >> e[i].u >> e[i].v >> e[i].t >> e[i].c;
    g(calc(1,0),calc(0,1));
    cout << q.first << ' ' << q.second << '\n';
    for (auto e:res)
        cout << e.u << ' ' << e.v << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 4 ms 596 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 3 ms 340 KB Output is correct
16 Correct 47 ms 340 KB Output is correct
17 Correct 49 ms 340 KB Output is correct
18 Correct 46 ms 340 KB Output is correct
19 Correct 442 ms 640 KB Output is correct
20 Correct 422 ms 644 KB Output is correct