This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Cay ngay cay dem nhung deo duoc cong nhan
*/
struct Edge {
int u, v, w;
Edge() {}
Edge(int u, int v, int w) : u(u), v(v), w(w) {}
};
bool cmp(Edge &a, Edge &b) { return a.w > b.w; }
const int oo = 1e9 + 5;
const int N = 1e5 + 5;
int n, m, q, k, d[N], p[N], pos[N], mine[N][17], LOG[N];
vpi adj[N];
priority_queue<pi, vpi, greater<pi> > pq;
vector<Edge> e, e1;
vpi d1[N];
int getp(int i) { return i == p[i]? i : p[i] = getp(p[i]); }
void join(int x, int y, int w) {
x = getp(x);
y = getp(y);
if (x == y) return;
if (sz(d1[x]) < sz(d1[y])) swap(x, y);
d1[x].back().nd = w;
EACH(i, d1[y]) d1[x].pb(i);
p[y] = x;
}
int get(int l, int r) {
if (l > r) swap(l, r);
--r;
int len = LOG[r - l + 1];
return min(mine[l][len], mine[r - (1 << len) + 1][len]);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> m;
FOR(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
e1.pb(Edge(u, v, w));
}
FOR(i, 1, n) d[i] = oo;
cin >> k;
FOR(i, 1, k) {
int u;
cin >> u;
d[u] = 0;
pq.push({0, u});
}
WHILE(!pq.empty()) {
pi cur = pq.top();
pq.pop();
int u = cur.nd, du = cur.st;
if (du > d[u]) continue;
EACH(j, adj[u]) {
int v = j.st, w = j.nd;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
pq.push({d[v], v});
}
}
}
EACH(i, e1) {
int u = i.u, v = i.v;
e.pb(Edge(u, v, min(d[u], d[v])));
}
sort(all(e), cmp);
FOR(i, 1, n) {
p[i] = i;
d1[i].pb({i, oo});
}
EACH(i, e) {
int u = i.u, v = i.v, w = i.w;
join(u, v, w);
}
int root = getp(1);
vpi a = d1[root];
reverse(all(a));
a.pb({0, 0});
reverse(all(a));
FOR(i, 1, n) {
pos[a[i].st] = i;
mine[i][0] = a[i].nd;
}
FOR(j, 1, 16)
FOR(i, 1, n) {
if (i + (1 << j) > n) continue;
int f = mine[i][j - 1], s = mine[i + (1 << (j - 1))][j - 1];
mine[i][j] = min(f, s);
}
FOR(i, 1, n) LOG[i] = log2(i);
cin >> q;
WHILE(q--) {
int s, t;
cin >> s >> t;
cout << get(pos[s], pos[t]) << '\n';
}
return 0;
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
Compilation message (stderr)
plan.cpp: In function 'void join(int, int, int)':
plan.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
plan.cpp:56:2: note: in expansion of macro 'EACH'
56 | EACH(i, d1[y]) d1[x].pb(i);
| ^~~~
plan.cpp: In function 'int main()':
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:70:5: note: in expansion of macro 'FOR'
70 | FOR(i, 1, m) {
| ^~~
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:77:5: note: in expansion of macro 'FOR'
77 | FOR(i, 1, n) d[i] = oo;
| ^~~
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:79:5: note: in expansion of macro 'FOR'
79 | FOR(i, 1, k) {
| ^~~
plan.cpp:26:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
plan.cpp:90:6: note: in expansion of macro 'EACH'
90 | EACH(j, adj[u]) {
| ^~~~
plan.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
plan.cpp:98:5: note: in expansion of macro 'EACH'
98 | EACH(i, e1) {
| ^~~~
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:103:5: note: in expansion of macro 'FOR'
103 | FOR(i, 1, n) {
| ^~~
plan.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
plan.cpp:107:5: note: in expansion of macro 'EACH'
107 | EACH(i, e) {
| ^~~~
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:116:5: note: in expansion of macro 'FOR'
116 | FOR(i, 1, n) {
| ^~~
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:120:5: note: in expansion of macro 'FOR'
120 | FOR(j, 1, 16)
| ^~~
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:121:5: note: in expansion of macro 'FOR'
121 | FOR(i, 1, n) {
| ^~~
plan.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
plan.cpp:126:5: note: in expansion of macro 'FOR'
126 | FOR(i, 1, n) LOG[i] = log2(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... |