CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:jmarttinen
Submission time:2022-11-11 22:37:41 +0200
Language:C++11
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'tpl next()':
input/code.cpp:23:12: error: 'LLONG_MAX' was not declared in this scope
   23 |     ll m = LLONG_MAX;
      |            ^~~~~~~~~
input/code.cpp:4:1: note: 'LLONG_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    3 | #include <queue>
  +++ |+#include <climits>
    4 | #include <vector>
input/code.cpp: In function 'int main()':
input/code.cpp:84:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for (int i=0; i<n-harbors.size(); i++) {
      |                   ~^~~~~~~~~~~~~~~~~

Code

#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using tpl = tuple<int, int, ll>;
vector<int> visited;

class Compare {
    public:
        bool operator() (tpl a, tpl b) {return (get<2>(a) < get<2>(b) && !visited[get<1>(a)]) || visited[get<1>(b)];}
};
using pqueue = priority_queue<tpl, vector<tpl>, Compare>;
vector<int> harbors;
int n;
map<int, pqueue> reachableFrom;
map<int, vector<tpl>> network;
map<int,ll> roadLengths; 

tpl next() {
    ll m = LLONG_MAX;
    map<int,pqueue>::iterator best;
    for (auto it = reachableFrom.begin(); it!= reachableFrom.end(); it++) {
        auto x = (it->second).top();
        // ll t = get<2>((it->second).top());
        while (visited[get<1>(x)]) {
            (it->second).pop();
            if ((it->second).empty())
                break;
            x = (it->second).top();
        } 
        if (get<2>(x) < m && !visited[get<1>(x)]) {
            m = get<2>(x);
            best = it;
        }
    }
    tpl return_val = (best->second).top();
    (best->second).pop();
    
    roadLengths[get<1>(return_val)] = get<2>(return_val);
    for (auto x : network[get<1>(return_val)]) {
        if (get<1>(x) != get<0>(return_val)) {
            reachableFrom[best->first].push({get<0>(x), get<1>(x), roadLengths[get<0>(x)] + get<2>(x)});
        }
        
    }
    return return_val;
}


int main() {
    int a,b,t;
    ll c;
    

    cin >> n;
    visited = vector<int>(n, 0);
    for (int i=0; i<n; i++) {
        cin >> t;
        network[i] = vector<tpl>();
        roadLengths[i] = 0;
        if (t==0) {
            harbors.push_back(i);
            reachableFrom[i] = pqueue();
            visited[i] = 1;
        }
    }
    
    for (int i=0; i<n-1; i++) {
        cin >> a >> b >> c; a--; b--;
        network[a].push_back({a,b,c});
        network[b].push_back({b,a,c});

        if (reachableFrom.count(a)) {
            reachableFrom[a].push({a,b,c});}
        if (reachableFrom.count(b)) {
            reachableFrom[b].push({b,a,c});}
        
    }
    tpl T;
    ll total = 0;
    for (int i=0; i<n-harbors.size(); i++) {
        T = next();
        visited[get<1>(T)] = 1;
    }
    for (int i=0; i<n; i++) {
        total += roadLengths[i];
    }
    cout << total;
}