This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee //
// 271828___182845__904523__53602__ //
// 87___47____13______52____66__24_ //
// 97___75____72______47____09___36 //
// 999595_____74______96____69___67 //
// 62___77____24______07____66__30_ //
// 35___35____47______59____45713__ //
// eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee //
#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>
#include <cstdio>
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, isok;
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 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), swap(u, v);
par[b] = a;
sizes[a] += sizes[b];
isok[b] = min(mind[v], mind[u]);
}
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);
}
}
bool check(ll mnd, int x, int y) {
if (mind[x] < mnd || mind[y] < mnd) return 0;
int p1 = x, p2 = y;
while (isok[x] >= mnd && x != par[x]) x = par[x];
while (isok[y] >= mnd && y != par[y]) y = par[y];
return x == y;
}
int main() {
scanf("%d%d", &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; scanf("%d%d%d", &u, &v, &w);
gp[--u].push_back({ --v, w });
gp[v].push_back({ u, w });
vp_.push_back({ {0, 0}, {u, v} });
}
scanf("%d", &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) scanf("%d", &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);
isok = par = sizes = vector <ll>(n, 1);
for (int i = 0; i < n; ++i) par[i] = i;
build_tree();
scanf("%d", &q);
while (q--) {
int s, t; scanf("%d%d", &s, &t); --s, --t;
ll l = 0, r = 1e15 + 5;
while (l + 1 < r) {
ll m = (l + r) / 2;
if (!check(m, s, t)) r = m;
else l = m;
}
printf("%d\n", l);
}
}
Compilation message (stderr)
plan.cpp: In function 'void build_tree()':
plan.cpp:58: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]
58 | for (int i = 0; i < vp_.size(); ++i) {
| ~~^~~~~~~~~~~~
plan.cpp: In function 'bool check(ll, int, int)':
plan.cpp:70:9: warning: unused variable 'p1' [-Wunused-variable]
70 | int p1 = x, p2 = y;
| ^~
plan.cpp:70:17: warning: unused variable 'p2' [-Wunused-variable]
70 | int p1 = x, p2 = y;
| ^~
plan.cpp: In function 'int main()':
plan.cpp:89:41: warning: format '%d' expects argument of type 'int*', but argument 2 has type '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type*' {aka 'long long int*'} [-Wformat=]
89 | for (int i = 0; i < k; ++i) scanf("%d", &g[i]), --g[i], pq.push({0, g[i]}), mind[g[i]] = 0;
| ~^
| |
| int*
| %lld
plan.cpp:102: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]
102 | for (int i = 0; i < vp_.size(); ++i) {
| ~~^~~~~~~~~~~~
plan.cpp:121:18: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll' {aka 'long long int'} [-Wformat=]
121 | printf("%d\n", l);
| ~^ ~
| | |
| int ll {aka long long int}
| %lld
plan.cpp:77:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
plan.cpp:81:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | int u, v, w; scanf("%d%d%d", &u, &v, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
plan.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
plan.cpp:89:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | for (int i = 0; i < k; ++i) scanf("%d", &g[i]), --g[i], pq.push({0, g[i]}), mind[g[i]] = 0;
| ~~~~~^~~~~~~~~~~~~
plan.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
plan.cpp:114:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | int s, t; scanf("%d%d", &s, &t); --s, --t;
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |