제출 #940540

#제출 시각아이디문제언어결과실행 시간메모리
940540KK_1729Evacuation plan (IZhO18_plan)C++17
23 / 100
908 ms104076 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define pb push_back #define all(a) a.begin(), a.end() // #define endl "\n" void printVector(vector<int> a){ for (auto x: a) cout << x << " "; cout << endl; } int N = 0; vector<vector<pair<int, int>>> graph; vector<int> distance(vector<int> source){ // return ; using T = pair<long long, int>; priority_queue<T, vector<T>, greater<T>> pq; // stores dist, node vector<int> dist(N+1, 1e16); for (auto x: source){ pq.push({0, x}); dist[x] = 0; } while (!pq.empty()){ auto [cdist, node] = pq.top(); pq.pop(); if (cdist != dist[node]) continue; for (auto x: graph[node]){ if (cdist + x.second < dist[x.first]){ pq.push({dist[x.first] = cdist + x.second, x.first}); } } } return dist; } struct UFDS { vector<int> link, siz; UFDS(int n) : link(n), siz(n, 1) { iota(link.begin(), link.end(), 0); } //each element as its own subset, with size 1 int findRoot(int x) { return x == link[x] ? x : link[x] = findRoot(link[x]); } bool same(int x, int y) { return findRoot(x) == findRoot(y); } bool unite(int x, int y) { x = findRoot(x); y = findRoot(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; link[y] = x; return true; } int size(int x) { return siz[findRoot(x)]; } }; int min(int x, int y){ if (x < y) return x; else return y; } void solve(){ int n, m; cin >> n >> m; N = n; graph.resize(n+1); FOR(i,0,m){ int a, b, w; cin >> a >> b >> w; graph[a].pb({b, w}); graph[b].pb({a, w}); } int k; cin >> k; vector<int> np(k); FOR(i,0,k) cin >> np[i]; vector<int> d = distance(np); vector<vector<int>> new_graph(n+1); vector<vector<int>> edges; FOR(i,1,n+1){ for (auto x: graph[i]){ edges.pb({min(d[x.first], d[i]), i, x.first}); } } UFDS ds(n+1); sort(all(edges)); reverse(all(edges)); vector<vector<pair<int, int>>> maxtree(n+1); for (auto edge: edges){ // printVector(edge); if (!ds.same(edge[1], edge[2])){ ds.unite(edge[1], edge[2]); maxtree[edge[1]].pb({edge[2], edge[0]}); maxtree[edge[2]].pb({edge[1], edge[0]}); } } // FOR(i,1,n+1){ // for (auto x: maxtree[i]) cout << x.first << x.second << endl; // cout << endl; // } int q; cin >> q; while (q--){ int a, b; cin >> a >> b; cout << min(d[a], d[b]) << endl; // stack<pair<int, int>> s; // s.push({a, 1e11}); // vector<int> visited(n+1); // while (!s.empty()){ // pair<int, int> current = s.top(); // s.pop(); // if (current.first == b){ // cout << current.second << endl; // break; // } // if (visited[current.first]) continue; // for (auto x: maxtree[current.first]){ // if (visited[x.first]) continue; // s.push({x.first, min(current.second, x.second)}); // } // visited[current.first] = 1; // } } } int32_t main(){ ios::sync_with_stdio(false);cin.tie(0); int t = 1; // cin >> t; while (t--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...