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>
#define speed \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define precision \
cout.precision(30); \
cerr.precision(10);
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>
#define forn(n) for (int i = 1; i <= n; i++)
#define forlr(l, r) for (int i = l; i != r; (l > r ? i-- : i++))
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define mp(x, y) make_pair(x, y)
#define all(x) x.begin(), x.end()
#define pc(x) __builtin_popcount(x)
#define pcll(x) __builtin_popcountll(x)
#define F first
#define S second
#define int ll
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void ioi(string name) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
int n, m, k, z, d[500005], p[500005], sz[500005];
vector<int> w, c[500005], r[500005];
vector<pair<int, int>> g[500005];
priority_queue<pair<int, int>> s;
pair<pair<int, int>, pair<int, int>> q[500005];
int get(int v) { return p[v] = (v == p[v] ? v : get(p[v])); }
void add(int u, int v) {
if ((u = get(u)) == (v = get(v))) return;
if (sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v], p[v] = u;
}
main() {
speed;
precision;
// code
cin >> n >> m;
for (int i = 1; i <= n; i++) {
d[i] = 1000000000;
}
for (int i = 1; i <= m; i++) {
int u, v, c;
cin >> u >> v >> c;
g[u].pb(mp(v, c));
g[v].pb(mp(u, c));
}
cin >> k;
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
d[x] = 0;
s.push(mp(0, x));
}
while (sz(s)) {
int v = s.top().S, t = -s.top().F;
s.pop();
if (t > d[v]) continue;
for (auto [to, cost] : g[v]) {
if (d[to] > d[v] + cost) {
d[to] = d[v] + cost;
s.push(mp(-d[to], to));
}
}
}
for (int i = 1; i <= n; i++) w.pb(d[i]), c[d[i]].pb(i);
sort(all(w));
w.erase(unique(all(w)), w.end());
cin >> z;
for (int i = 1; i <= z; i++) {
int u, v;
cin >> u >> v;
q[i] = mp(mp(0, sz(w) - 1), mp(u, v));
}
while (true) {
int ok = 0;
for (int i = 1; i <= z; i++) {
if (q[i].F.F != q[i].F.S) {
ok = 1;
r[(q[i].F.F + q[i].F.S) / 2].pb(i);
}
}
if (!ok) break;
for (int i = 1; i <= n; i++) sz[i] = 1, p[i] = i;
for (int i = sz(w) - 1; i >= 0; i--) {
for (auto j : c[w[i]]) {
for (auto k : g[j]) {
if (w[i] <= d[k.F]) add(j, k.F);
}
}
for (auto j : r[i]) {
if (get(q[j].S.F) != get(q[j].S.S))
q[j].F.S = i;
else
q[j].F.F = i + 1;
}
r[i].clear();
}
}
for (int i = 1; i <= z; i++) cout << w[q[i].F.F - 1] << "\n";
// endl
#ifndef ONLINE_JUDGE
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
Compilation message (stderr)
plan.cpp:41:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
41 | main() {
| ^
plan.cpp: In function 'int main()':
plan.cpp:66:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for (auto [to, cost] : g[v]) {
| ^
plan.cpp: In function 'void ioi(std::string)':
plan.cpp:27:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
27 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
28 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |