Submission #956922

# Submission time Handle Problem Language Result Execution time Memory
956922 2024-04-02T16:53:26 Z bunhadasou timeismoney (balkan11_timeismoney) C++14
100 / 100
109 ms 1176 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define PB push_back
#define EB emplace_back
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
#define ll long long
#define sz(x) (int)x.size()
#define TASK "cf"



using namespace std;

const double maxx=1e5;
const int maxn=222;


struct data{
    ll u,v,t,c;
    double w;
};

vector<data> e;
int n,m;
int par[maxn],Rank[maxn];

int find(int x) {
    if (x==par[x]) return x; return par[x]=find(par[x]);
}

bool UNION(int x,int y) {
    x=find(x);y=find(y);
    if (x==y) return 0;
    if (Rank[x]<Rank[y]) swap(x,y);
    Rank[x]+=Rank[y];
    par[y]=x;
    return 1;

}

bool cmp(data a,data b) {
    return a.w<b.w;
}

void init(ll x){
    for (int i=0;i<=n;i++) {par[i]=i;Rank[i]=1;}
    for (int i=0;i<m;i++) {
        e[i].w=e[i].c*x+e[i].t*(maxx-x);
    }
    sort(all(e),cmp);
}

ll solve(double mid) {
    init(mid);
    int solt=0,solc=0;
    for (int i=0;i<m;i++) {
        if (UNION(e[i].u,e[i].v)){
            solt+=e[i].t;
            solc+=e[i].c;
        }
    }
    return 1ll*solc*solt;
}

int main(){
 	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    cin>>n>>m;
    for (int i=1;i<=m;i++) {
        int u,v,x,y; cin>>u>>v>>x>>y;
        e.PB({u,v,x,y,0});
    }
    double lo=0,hi=maxx;
    for (int rev=1;rev<=70;rev++) {
        double m1=(2*lo+hi)/3;
        double m2=(lo+2*hi)/3;
        if (solve(m1)<solve(m2)) hi=m2;else lo=m1;
    }
    init(lo);
    vector<pair<int,int>> ans;
    int sumc=0,sumt=0;
    for (int i=0;i<m;i++) {
        if (UNION(e[i].u,e[i].v)) {
            sumc+=e[i].c;sumt+=e[i].t;
            ans.PB(mp(e[i].u,e[i].v));
        }
    }
    cout<<sumt<<" "<<sumc<<"\n";
    for (auto x:ans) cout<<x.fi<<" "<<x.se<<"\n";


    return 0;
}

Compilation message

timeismoney.cpp: In function 'int find(int)':
timeismoney.cpp:31:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   31 |     if (x==par[x]) return x; return par[x]=find(par[x]);
      |     ^~
timeismoney.cpp:31:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   31 |     if (x==par[x]) return x; return par[x]=find(par[x]);
      |                              ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 14 ms 604 KB Output is correct
8 Correct 79 ms 1176 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 3 ms 344 KB Output is correct
15 Correct 3 ms 348 KB Output is correct
16 Correct 18 ms 644 KB Output is correct
17 Correct 18 ms 604 KB Output is correct
18 Correct 18 ms 600 KB Output is correct
19 Correct 109 ms 1176 KB Output is correct
20 Correct 107 ms 1172 KB Output is correct