답안 #480868

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
480868 2021-10-18T13:58:46 Z duchung 시간이 돈 (balkan11_timeismoney) C++17
100 / 100
269 ms 1352 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double
#define ii pair<int , int>
#define dd pair<double , double>
#define iiii pair<ii , dd>

const int N = 205;
const double EPS = 1e-9;

int n , m;
vector<iiii> edges;
double tmp , slope;
int parent[N] , level[N];

double val(iiii tmp)
{
    return slope * tmp.second.first + (1 - slope) * tmp.second.second;
}

bool cmp(iiii x , iiii y)
{
    return val(x) + EPS < val(y);
}

int find_set(int u)
{
    return u == parent[u] ? u : parent[u] = find_set(parent[u]);
}

bool union_set(int u , int v)
{
    u = find_set(u);
    v = find_set(v);

    if (u == v) return false;

    if (level[u] < level[v]) swap(u , v);
    parent[v] = u;
    if (level[u] == level[v]) ++level[u];

    return true;
}

int solve(double cur)
{
    slope = cur;
    sort(edges.begin() , edges.end() , cmp);
    for (int i = 0 ; i < n ; ++i) parent[i] = i;
    memset(level , 0 , sizeof(level));

    int sumT = 0 , sumC = 0;
    for (auto x : edges)
    {
        int u = x.first.first;
        int v = x.first.second;
        int t = x.second.first;
        int c = x.second.second;

        if (union_set(u , v))
        {
            sumT += t;
            sumC += c;
        }
    }
    return sumT * sumC;
}

void print(double cur)
{
    slope = cur;
    sort(edges.begin() , edges.end() , cmp);
    for (int i = 0 ; i < n ; ++i) parent[i] = i;
    memset(level , 0 , sizeof(level));

    int sumT = 0 , sumC = 0;
    vector<pair<int , int>> ans;
    for (auto x : edges)
    {
        int u = x.first.first;
        int v = x.first.second;
        int t = x.second.first;
        int c = x.second.second;

        if (union_set(u , v))
        {
            sumT += t;
            sumC += c;
            ans.push_back({u , v});
        }
    }

    cout << sumT << " " << sumC << "\n";
    for (auto x : ans) cout << x.first << " " << x.second << "\n";
}

signed main()
{
    // freopen(".inp","r",stdin);
    // freopen(".out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    while(m--)
    {
        int u , v , t , c;
        cin >> u >> v >> t >> c;
        edges.push_back({{u , v} , {t , c}});
    }

    double l = 0 , r = 1;
    while(r - l > 1e-8)
    {
        double lm = l + (r - l) / 3;
        double rm = r - (r - l) / 3;
        if (solve(lm) > solve(rm))
        {
            l = lm;
        }
        else
        {
            r = rm;
        }
        // cerr << l << " " << r << "\n";
    }

    if (solve(l) > solve(r))
    {
        print(r);
    }
    else
    {
        print(l);
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 8 ms 332 KB Output is correct
6 Correct 5 ms 332 KB Output is correct
7 Correct 35 ms 460 KB Output is correct
8 Correct 198 ms 1352 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 2 ms 316 KB Output is correct
13 Correct 2 ms 312 KB Output is correct
14 Correct 8 ms 372 KB Output is correct
15 Correct 7 ms 332 KB Output is correct
16 Correct 51 ms 460 KB Output is correct
17 Correct 46 ms 460 KB Output is correct
18 Correct 43 ms 460 KB Output is correct
19 Correct 268 ms 1352 KB Output is correct
20 Correct 269 ms 1352 KB Output is correct