Submission #124927

#TimeUsernameProblemLanguageResultExecution timeMemory
124927NMAC427관광 (NOI14_sightseeing)C++17
0 / 25
2148 ms114340 KiB
// 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"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...