CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:qanpi
Submission time:2022-11-06 16:09:49 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp:10:17: error: 'INT_MAX' was not declared in this scope
   10 | const int INF = INT_MAX-1;
      |                 ^~~~~~~
input/code.cpp:4:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    3 | #include<queue>
  +++ |+#include <climits>
    4 |

Code

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

using namespace std;

int n;
bool field[2*100000+1];
vector<pair<int, int>> adj[2*100000+1];
const int INF = INT_MAX-1;

int djikstra(int x);

int main() {
	cin >> n;

	for(int i=1; i<=n; i++) {
		cin >> field[i];	
	}

	for(int i=0; i<n-1; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		adj[a].push_back({b, c});
		adj[b].push_back({a, c});
	}

	int sum = 0;
	for(int i=1; i<=n; i++) {
		sum += djikstra(i);
	}
	cout << sum << endl;
}

int djikstra(int x) {
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
	bool visited[2*100000+1] = {};
	int distance[2*100000+1];

	for (int i=0; i<n; i++) {
		distance[i] = INF;
	}

	distance[x] = 0;
	q.push({0, x});

	int shortest = INF;
	while (!q.empty()) {
		int a = q.top().second, w = q.top().first; q.pop();

		if(visited[a]) continue;
		visited[a] = true;

		if(!field[a]) shortest = w < shortest ? w : shortest;

		bool allFar = true;
		for (auto u : adj[a]) {
			int b = u.first, c = u.second;
			
			if(c + w < shortest) allFar = false;
			if(distance[a]+c < distance[b]) {
				distance[b] = distance[a]+c;
				q.push({distance[b], b});
			}
		}

		if (allFar) {
			return shortest;
		}
	}

	return shortest;
}