CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:FenixHongell
Submission time:2022-11-09 08:46:02 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In instantiation of 'void Graph<T>::djikstraSSSP(T, std::vector<int>&) [with T = int]':
input/code.cpp:110:16:   required from here
input/code.cpp:28:39: error: 'std::map<int, std::__cxx11::list<std::pair<int, int>, std::allocator<std::pair<int, int> > >, std::less<int>, std::allocator<std::pair<const int, std::__cxx11::list<std::pair<int, int>, std::allocator<std::pair<int, int> > > > > >::mapped_type' {aka 'class std::__cxx11::list<std::pair<int, int>, std::allocator<std::pair<int, int> > >'} has no member named 'first'
   28 |                         T node = l[j].first;
      |                                  ~~~~~^~~~~
input/code.cpp:45:55: error: no match for 'operator[]' (operand types are 'std::map<int, std::__cxx11::list<std::pair<int, int>, std::allocator<std::pair<int, int> > >, std::less<int>, std::allocator<std::pair<const int, std::__cxx11::list<std::pair<int, int>, std::allocator<std::pair<int, int> > > > > >::mapped_type' {aka 'std::__cxx11::list<std::p...

Code

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <regex>
#include <numeric>
#include <algorithm>
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;
		unsigned int j = 0;
		while (j < l.size()) {
			T node = l[j].first;
			dist[node] = INT_MAX;
			++j;
		}
		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;
			unsigned int i = 0;
			while (i < l[currNode].size()) {
				auto nbr = l[currNode][i];
				T nbrNode = nbr.first;
				int distInBetween = nbr.second;
				int nbrNodeDist = dist[nbrNode];

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

		}
		unsigned int i = 0;
		while (i < dist.size()) {
			auto x = dist[i];
			if (scores[i] != 0) {
				if (scores[i] > x.second) {
					scores[i] = x.second;
				}
			}
			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;
	std::string::difference_type rl = std::count(roles.begin(), roles.end(), '0');
	while (j < roles.length() + 1) {
		if (roles[j - 1] == '0') {

			g.addEdge(0, j, 0);
		}
		++j;
	}
	vector<int> scores(V + rl, 0);
	g.djikstraSSSP(0, scores);

	int totalLength = std::reduce(scores.begin(), scores.end());
	std::cout << totalLength << endl;
}