This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_map>
#include <cmath>
#include <climits>
#include <algorithm>
#include <random>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <tuple>
#include <bitset>
#include <chrono>
#include <ctime>
#include <fstream>
#include <stack>
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
const ll mod = 1e9 + 7, inf = 1e18;
int n, m, k, q;
vector<vector<pair<int, ll>>> gp;
vector<vector<int>> tree;
vector<ll> g, mind, par, sizes, d;
vector<pair<ll, int>> vp;
vector<pair<pair<int, int>, pair<int, int>>> vp_;
int get(int u) {
if (u == par[u]) return u;
return par[u] = get(par[u]);
}
void union_sets(int u, int v) {
int a = get(u), b = get(v);
if (a == b) return;
if (sizes[a] < sizes[b])swap(a, b);
par[b] = a;
sizes[a] += sizes[b];
}
void build_tree() {
for (int i = 0; i < vp_.size(); ++i) {
int u = vp_[i].second.first, v = vp_[i].second.second;
if (get(u) == get(v)) continue;
tree[u].push_back(v);
tree[v].push_back(u);
union_sets(u, v);
}
}
void dfs(int u, int p) {
d[u] = min(d[u], mind[u]);
for (int& v : tree[u]) {
//cout << "! " << v << endl;
if (v != p) {
d[v] = min(d[u], mind[v]);
dfs(v, u);
}
}
}
void solve() {
cin >> n >> m;
gp = vector<vector<pair<int, ll>>>(n);
mind = vector<ll>(n, 1e18);
for (int i = 0; i < m; ++i) {
int u, v, w; cin >> u >> v >> w;
gp[--u].push_back({ --v, w });
gp[v].push_back({ u, w });
vp_.push_back({ {0, 0}, {u, v} });
}
cin >> k;
g = vector<ll>(k);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
for (int i = 0; i < k; ++i) cin >> g[i], --g[i], pq.push({ 0, g[i] }), mind[g[i]] = 0;
while (pq.size()) {
pair<ll, int> p = pq.top();
pq.pop();
ll d = p.first, u = p.second;
if (mind[u] < d) continue;
for (pair<int, ll> v : gp[u]) {
if (mind[v.first] > mind[u] + v.second) {
mind[v.first] = mind[u] + v.second;
pq.push({ mind[v.first], v.first });
}
}
}
for (int i = 0; i < vp_.size(); ++i) {
int u = vp_[i].second.first, v = vp_[i].second.second;
if (mind[u] > mind[v]) swap(u, v);
vp_[i] = { {mind[u], mind[v]}, {u, v} };
}
sort(vp_.rbegin(), vp_.rend());
tree = vector<vector<int>>(n);
par = sizes = vector <ll> (n, 1);
for (int i = 0; i < n; ++i) par[i] = i;
build_tree();
cin >> q;
while (q--) {
int s, t; cin >> s >> t; --s, --t;
d = vector<ll>(n, inf);
dfs(s, -1);
cout << d[t] << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
//int _; cin >> _; while (_--)
solve();
}
Compilation message (stderr)
plan.cpp: In function 'void build_tree()':
plan.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < vp_.size(); ++i) {
| ~~^~~~~~~~~~~~
plan.cpp: In function 'void solve()':
plan.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < vp_.size(); ++i) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |