CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:chaotic
Submission time:2022-11-02 22:05:03 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp:19:24: error: 'LLONG_MAX' was not declared in this scope
   19 |     ll shortest_down = LLONG_MAX;
      |                        ^~~~~~~~~
input/code.cpp:7:1: note: 'LLONG_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    6 | #include <sstream>
  +++ |+#include <climits>
    7 | #include <vector>
input/code.cpp:57:19: error: 'LLONG_MAX' was not declared in this scope
   57 |     ll shortest = LLONG_MAX;
      |                   ^~~~~~~~~
input/code.cpp:57:19: note: 'LLONG_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
input/code.cpp: In member function 'void City::calc_down()':
input/code.cpp:40:16: error: 'LLONG_MAX' was not declared in this scope
   40 |         ll l = LLONG_MAX;
      |                ^~~~~~~~~
input/code.cpp:40:16: note: 'LLONG_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
input/code.cpp: In lambda function:
input/code.cpp:51:22: error: 'LLONG_MAX' is...

Code

#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <numeric>
#include <sstream>
#include <vector>

using ll = long long;

struct City {
    City* left  = nullptr;
    City* right = nullptr;
    ll left_dist;
    ll right_dist;

    bool port;

    ll shortest_down = LLONG_MAX;
    bool has_parent  = false;

    void connect(City& o, ll dist) {
        assert(o.has_parent == false);
        if (!left) {
            left      = &o;
            left_dist = dist;
        } else {
            assert(!right);
            right      = &o;
            right_dist = dist;
        }
        o.has_parent = true;
    }

    void calc_down() {
        if (port) {
            shortest_down = 0;
            return;
        }
        ll l = LLONG_MAX;
        ll r = LLONG_MAX;
        if (left) {
            left->calc_down();
            l = left->shortest_down;
        }
        if (right) {
            right->calc_down();
            r = right->shortest_down;
        }
        auto add = [](ll a, ll b) {
            if (a == LLONG_MAX || b == LLONG_MAX) return LLONG_MAX;
            return a + b;
        };
        shortest_down = std::min(add(l, left_dist), add(r, right_dist));
    }

    ll shortest = LLONG_MAX;
    void calc_shortest(ll up_dist, City* p) {
        if (!p) {
            shortest = shortest_down;
        } else if (port) {
            shortest = 0;
        } else {
            auto add = [](ll a, ll b) {
                if (a == LLONG_MAX || b == LLONG_MAX) return LLONG_MAX;
                return a + b;
            };
            ll l     = left ? add(left->shortest_down, left_dist) : LLONG_MAX;
            ll r     = right ? add(right->shortest_down, right_dist) : LLONG_MAX;
            shortest = std::min(std::min(l, r), add(p->shortest, up_dist));
        }
        if (left) left->calc_shortest(left_dist, this);
        if (right) right->calc_shortest(right_dist, this);
    }
};

int main() {
    std::cin.sync_with_stdio(false);
    //    auto& stream = std::cin;

    std::stringstream ss{ R"(6
                         1 1 0 0 1 1
                         1 2 20
                         2 3 30
                         2 5 50
                         3 6 60
                         1 4 10)" };
    ss.copyfmt(std::cin);
    ss.exceptions(ss.badbit | ss.failbit);
    auto& stream = ss;

    int n;
    stream >> n;

    std::vector<City> cities;
    cities.reserve(n);
    for (int i = 0; i < n; ++i) {
        //
        int v;
        stream >> v;
        City nw;
        nw.port = v == 0 ? true : false;
        cities.push_back(nw);
    }
    for (int i = 0; i < n - 1; ++i) {
        int a, b, c;
        stream >> a >> b >> c;
        cities[a - 1].connect(cities[b - 1], c);
    }

    City* root;
    for (auto& c : cities) {
        if (c.has_parent == false) {
            root = &c;
            break;
        }
    }
    std::cout << root << "\n";
    root->calc_down();
    root->calc_shortest(LLONG_MAX, nullptr);
    std::cout << root->shortest_down << "\n";
    ll sum = 0;
    for (auto& c : cities) {
        std::cout << c.shortest << "\n";
        sum += c.shortest;
    }
    std::cout << "\n" << sum << "\n";
}