답안 #124927

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124927 2019-07-04T07:23:02 Z NMAC427 관광 (NOI14_sightseeing) C++17
0 / 25
2148 ms 114340 KB
// https://oj.uz/problem/view/NOI14_sightseeing

#include <bits/stdc++.h>

#define int int_fast32_t
#define coutV(x) for (const auto& e: x) {cerr << e << " ";} cerr << "\n"

using namespace std;


class UnionFind {

	vector<int> p;

public:

	UnionFind(size_t size) {
		p.resize(size);
		iota(p.begin(), p.end(), 0);
	}

	int parent(int v) {
		return p[v] = (p[v] == v ? v : parent(p[v]));
	}

	void unite(int a, int b) {
		if (rand() % 2) swap(a, b);
		p[parent(a)] = parent(b);
	}

};


int V, E, Q;

signed main() {
	
	#ifndef __APPLE__
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	#endif

	cin >> V >> E >> Q;

	vector<pair<int, pair<int, int>>> edges;
	edges.resize(E);

	for (int i = 0; i < E; i++) {
		cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first;
	}

	// MST
	sort(edges.rbegin(), edges.rend());
	
	UnionFind uf (V);
	vector<vector<pair<int, int>>> adj;
	adj.resize(V);

	for (const auto& e: edges) {
		if (uf.parent(e.second.first) == uf.parent(e.second.second)) continue;
		uf.unite(e.second.first, e.second.second);

		adj[e.second.first].emplace_back(e.second.second, e.first);
		adj[e.second.second].emplace_back(e.second.first, e.first);
	}


	// Dijkstra
	vector<int> visited (V, 0);
	vector<bool> bVisited (V, false);

	int visitedCounter = 0;

	priority_queue<pair<int, int>> pq;
	pq.push({INT32_MAX, 0});

	while (!pq.empty() && visitedCounter < V) {
		auto top = pq.top();
		pq.pop();

		int v = top.second;
		int pathQ = top.first;

		if (bVisited[v] || visited[v] > pathQ) {
			continue;
		}

		visited[v] = pathQ;
		bVisited[v] = true;
		visitedCounter += 1;

		for (const auto& child: adj[v]) {
			int childQ = min(child.second, pathQ);
			if (visited[child.first] >= childQ) continue;
			visited[child.first] = childQ;
			pq.push({childQ, child.first});
		}
	}

	for (int i = 0; i < Q; i++) {
		int x;
		cin >> x;
		cout << visited[x - 1] << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 40 ms 5596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2148 ms 114340 KB Output isn't correct
2 Halted 0 ms 0 KB -