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;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int M = 5e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
vector<ii> adj[N];
int n, m, q, numNpp;
vector<int> Npps, val;
int d[N];
void read() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v, c; cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
cin >> numNpp;
Npps.assign(numNpp, 0);
for(int &i: Npps) cin >> i;
}
void calDistToNpp() {
priority_queue<ii, vector<ii>, greater<ii>> heap;
memset(d, 0x3f, sizeof(d));
for(int i: Npps) {
heap.push({0, i});
d[i] = 0;
}
while(!heap.empty()) {
ii tmp = heap.top(); heap.pop();
int u = tmp.se;
int du = tmp.fi;
if (du != d[u]) continue;
for(auto [v, c]: adj[u]) {
if (minimize(d[v], d[u] + c)) {
heap.push(mp(d[v], v));
}
}
}
}
bool vis[N];
bool dfs(int u, int t, int lim) {
if (vis[u]) return false;
vis[u] = 1;
if (d[u] < lim) return false;
if (u == t) return true;
for(auto [v, c]: adj[u]) if (d[v] >= lim) {
if (dfs(v, t, lim)) return true;
}
return false;
}
bool check(int s, int t, int lim) {
fill(vis + 1, vis + n + 1, 0);
return dfs(s, t, lim);
}
void solve() {
calDistToNpp();
for(int i = 1; i <= n; i++) val.push_back(d[i]);
sort(ALL(val));
val.erase(unique(ALL(val)), val.end());
cin >> q;
while(q--) {
int s, t; cin >> s >> t;
int l = 0, r = SZ(val) - 1, ans = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if (check(s, t, val[mid])) {
ans = val[mid];
l = mid + 1;
}
else r = mid - 1;
}
cout << ans << endl;
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
// rmq - rmq2d
// hash
// fw - fw2d
// segtree
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(TASK".out", "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... |