CSES - Datatähti 2020 alku - Results
Submission details
Task:Mastot
Sender:JPaananen
Submission time:2019-10-13 19:51:12 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:75:2: error: 'memset' was not declared in this scope
  memset(cameFrom, 0, (dist + 1) * sizeof(int));
  ^~~~~~
input/code.cpp:75:2: note: suggested alternative: 'wmemset'
  memset(cameFrom, 0, (dist + 1) * sizeof(int));
  ^~~~~~
  wmemset

Code

#include <iostream>
#include <cstdlib>
#include <queue>

// UNCLEAN VERSION just to make sure it works :^)

using int_t = unsigned; // Don't judge me.
//using uint_t = unsigned long long;

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int dist;
	std::cin >> dist;

	int goal = dist;
	
	int* ds = (int*) malloc((dist + 1) * sizeof(int));
	//memset(ds, 0, (dist + 1) * sizeof(int));
	int* cs = (int*) malloc((dist + 1) * sizeof(int));
	//memset(cs, 0, (dist + 1) * sizeof(int));

	//std::cout << "Length of ds: " << (dist + 1) << std::endl;
	//std::cout << "Length of cs: " << (dist + 1) << std::endl;

	for (int i = 0; i < dist; ++i)
		std::cin >> ds[i];
	ds[dist] = 0;
	
	for (int i = 0; i < dist - 1; ++i)
		std::cin >> cs[i+1];
	cs[dist - 1] = 0;
	cs[dist] = 0;
	cs[0] = 0;

	//std::cout << "Reach map: [ ";
	//for (int i = 0; i < dist; ++i) {
	//	std::cout << ds[i];
	//	if (i == dist - 1)
	//		std::cout << " ]" << std::endl;
	//	else
	//		std::cout << ", ";
	//}

	//std::cout << "Cost map:  [ -, ";
	//for (int i = 1; i < dist; ++i) {
	//	std::cout << cs[i];
	//	if (i == dist - 1)
	//		std::cout << " ]" << std::endl;
	//	else
	//		std::cout << ", ";
	//}



	struct Node {
		Node(int _pos, int_t _priority) : pos(_pos), priority(_priority) {}
		int pos;
		int_t priority;
	};

	auto cmp = [](const Node& lhs, const Node& rhs) {
		return lhs.priority > rhs.priority;
	};

	std::vector<Node> container;
	container.reserve(dist);
	std::priority_queue<Node, decltype(container), decltype(cmp)> frontier(cmp, std::move(container));
	
	frontier.emplace(0, 0);

	int* cameFrom = (int*) malloc((dist + 1) * sizeof(int));
	memset(cameFrom, 0, (dist + 1) * sizeof(int));
	//std::cout << "Length of cameFrom: " << dist << std::endl;
	//memset(cameFrom, -1, nPylons * sizeof(int));
	cameFrom[0] = 0;

	constexpr int_t NOT_PRESENT = std::numeric_limits<int_t>::max()-1;
	//std::cout << NOT_PRESENT << std::endl;
	int_t* costSoFar = (int_t*) malloc((dist + 1) * sizeof(int_t));
	memset(costSoFar, NOT_PRESENT, (dist + 1) * sizeof(int_t));
	//std::cout << "Length of costSoFar: " << dist << std::endl;
	/*for (int i = 0; i < dist + 1; ++i)
		costSoFar[i] = NOT_PRESENT;*/

	costSoFar[0] = 0;

	auto heuristic = [](int goal, int next) -> int_t {
		//if (next >= goal)
		//	std::cout << "!!! Goal: " << goal << ", next: " << next << std::endl;
		return goal - next;
	};

	//int iter = 0;
	while (!frontier.empty()) {
		//std::cout << std::endl << "## Iteration " << (iter++) << " ##" << std::endl;

		Node curr = frontier.top(); 
		frontier.pop();
		
		int pos = curr.pos;
		//std::cout << "Popped node: " << pos << ". Cost so far: " << costSoFar[pos] << ", Reach: " << ds[pos] << ", Cost to Fix: " << cs[pos] << std::endl;

		if (pos == goal) {
			//std::cout << costSoFar[pos];

			//std::cout << "Found goal! Total cost: " << costSoFar[pos] << std::endl;
			//std::cout << pos;
			//do {
			//	//std::cout << "(Reading from cameFrom[" << pos << "])" << std::endl;
			//	pos = cameFrom[pos];
			//	//std::cout << "(Reading from costSoFar[" << pos << "])" << std::endl;
			//	std::cout << " -> " << pos;
			//} while (pos != 0);

			std::cout << costSoFar[pos] << std::endl;

			return 0;
		}

		int reach = ds[pos];
		for (int i = 1; i <= reach; ++i) {
			int nbPos = pos + i;
			if (nbPos > goal)
				break;

			//std::cout << "Examining neighbor: " << nbPos << ". Reach: " << ds[nbPos] << ", Cost to Fix: " << cs[nbPos] << std::endl;

			//std::cout << "{Reading from costSoFar[" << pos << "] and cs[" << nbPos << "]}" << std::endl;
			int_t newCost = costSoFar[pos] + cs[nbPos];
			//std::cout << "> New cost: " << newCost << std::endl;

			//std::cout << "{Reading from costSoFar[" << nbPos << "]}" << std::endl;
			int_t neighborCost = costSoFar[nbPos];
			//std::cout << "> Neighbor cost: " << neighborCost << std::endl;
			if (/*neighborCost == NOT_PRESENT || */newCost < neighborCost) {
				//std::cout << " -> Adding to queue! Total cost to neighbor: " << newCost << std::endl;

				//std::cout << "{Writing 'costSoFar[" << nbPos << "] = " << newCost << "'}" << std::endl;
				costSoFar[nbPos] = newCost;
				int_t priority = newCost + heuristic(goal, nbPos);

				frontier.emplace(nbPos, priority);
				//std::cout << "{Writing 'cameFrom[" << nbPos << "] = " << pos << "'}" << std::endl;
				cameFrom[nbPos] = pos;
			}
		}
	}
	//std::cout << "No path found!!!" << std::endl;
	return 0;
}