CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:FenixHongell
Submission time:2022-11-08 13:55:19 +0200
Language:C++11
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:116:32: error: 'reduce' is not a member of 'std'
  116 |         int totalLength = std::reduce(scores.begin(), scores.end());
      |                                ^~~~~~

Code

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <regex>
#include <numeric>
using namespace std;
template<typename T>

class Graph {
	map<T, list<pair<T, int>>> l;

public:
	void addEdge(T x, T y, int wt) {
		l[x].push_back({ y, wt });
		l[y].push_back({ x, wt });
	}
	void djikstraSSSP(T src, vector<int>& scores) {

		map<T, int> dist;

		for (auto p : l) {
			T node = p.first;
			dist[node] = INT_MAX;
		}
		dist[src] = 0;

		set<pair<int, T>> s;
		s.insert({ dist[src], src });

		while (!s.empty()) {

			pair<int, T> p = *s.begin();
			s.erase(s.begin());
			T currNode = p.second;
			int currNodeDist = p.first;

			for (auto nbr : l[currNode]) {
				T nbrNode = nbr.first;
				//int distInBetween = nbr.second;
				//int nbrNodeDist = dist[nbrNode];

				if (currNodeDist + nbr.second < dist[nbrNode]) {
					auto pr = s.find({ dist[nbrNode], nbrNode });
					if (pr != s.end()) {
						s.erase(pr);
					}
					dist[nbrNode] = currNodeDist + nbr.second;
					s.insert({ dist[nbrNode], nbrNode });
				}
			}

		}
		int i = 0;
		for (auto x : dist) {
			if (scores[i] != -1) {
				if (scores[i] > x.second) {
					scores[i] = x.second;
				}
			}
			else {
				if (x.second == -1) {
					scores[i] = 0;
				}
				else {
					scores[i] = x.second;
				}
			}

			++i;
		}
	}

};

int main() {

	Graph<int> g;

	unsigned int V = 0;
	cin >> V;
	int i = 0;
	int paths = V - 1;
	string roles;
	getline(cin >> ws, roles);
	std::regex r("\\s+");

	roles = std::regex_replace(roles, r, "");
	
	while (i < paths) {
		int a, b, c;
		cin >> a >> b >> c;
		g.addEdge(a, b, c);
		++i;
	}
	unsigned int j = 1;
	vector<int> srcs;
	while (j < roles.length() + 1) {
		if (roles[j - 1] == '0') {

			srcs.push_back(j);
		}
		++j;
	}

	for (int point : srcs) {
		g.addEdge(0, point, 0);
	}
	vector<int> scores(V+srcs.size(), -1);
	g.djikstraSSSP(0, scores);

	int totalLength = std::reduce(scores.begin(), scores.end());
	std::cout << totalLength << endl;
} //TODO The extra edges and stuff are messing with the answer so fix that :) Otherwise 10/10