CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:FenixHongell
Submission time:2022-11-07 21:43:18 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:96:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         while (j < roles.length() + 1) {
      |                ~~^~~~~~~~~~~~~~~~~~~~
input/code.cpp:105:26: error: no matching function for call to 'Graph<int>::addEdge(int, int&)'
  105 |                 g.addEdge(0, point);
      |                 ~~~~~~~~~^~~~~~~~~~
input/code.cpp:17:14: note: candidate: 'void Graph<T>::addEdge(T, T, int) [with T = int]'
   17 |         void addEdge(T x, T y, int wt) {
      |              ^~~~~~~
input/code.cpp:17:14: note:   candidate expects 3 arguments, 2 provided

Code

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <regex>
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 {
				scores[i] = x.second;
			}

			++i;
		}
	}

};

int main() {

	Graph<int> g;

	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, "");
	vector<int> scores(V, -1);
	while (i < paths) {
		int a, b, c;
		cin >> a >> b >> c;
		g.addEdge(a, b, c);
		++i;
	}
	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);
	}
	g.djikstraSSSP(0, scores);
	int totalLength = 0;
	for (int num : scores) {
		totalLength += num;
	}
	cout << totalLength << endl;

}