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;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a = b, 1 : 0;
}
const int MX = 1e5+5, LG = 18;
int N, M, K, Q, par[MX][LG], dep[MX];
ll dist[MX], jmp[MX][LG];
V<pi> adj[MX], eds;
vi tr[MX];
struct UF {
vi e;
UF(int n) : e(n, -1) {}
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool sameSet(int a, int b) { return find(a) == find(b); }
bool join(int a, int b) {
a = find(a), b = find(b);
if(a == b) return false;
if(e[a] > e[b]) swap(a,b);
e[a] += e[b]; e[b] = a;
return true;
}
};
void dfs(int u, int p, int d = 0) {
par[u][0] = p;
jmp[u][0] = min(dist[u], dist[p]);
dep[u] = d;
each(v, tr[u]) if(v != p) {
dfs(v, u, d+1);
}
}
pair<int,ll> Jmp(int u, int d) {
pair<int,ll> ans = {u, dist[u]};
F0R(i,LG) if(d>>i&1) {
ans = {par[ans.first][i], min(ans.second, jmp[ans.first][i])};
}
return ans;
}
ll get_ans(int s, int t) {
if(dep[s] < dep[t]) swap(s,t);
pair<int,ll> tmp = Jmp(s, dep[s] - dep[t]);
ll ans = tmp.second;
s = tmp.first;
R0F(i,LG) if(par[s][i] != par[t][i]) {
ans = min({ans, jmp[s][i], jmp[t][i]});
s = par[s][i]; t = par[t][i];
}
return min({ans, jmp[s][0], jmp[t][0]});
}
void solve() {
cin>>N>>M;
F0R(i,M) {
int u,v,w;
cin>>u>>v>>w;
adj[u].pb({v,w});
adj[v].pb({u,w});
eds.pb({u,v});
}
memset(dist, 0x3f, sizeof dist);
cin>>K;
pqg<pair<ll,int>> pq;
F0R(i,K) {
int u; cin>>u;
pq.push({dist[u] = 0, u});
}
while(sz(pq)) {
ll du = pq.top().first;
int u = pq.top().second;
pq.pop();
if(du != dist[u]) continue;
each(ed, adj[u]) {
int v = ed.first;
ll dv = du + ed.second;
if(ckmin(dist[v], dv)) pq.push({dist[v], v});
}
}
UF dsu(N+1);
sort(all(eds), [&](const pi&a, const pi&b){ return min(dist[a.first], dist[a.second]) > min(dist[b.first], dist[b.second]); });
F0R(i,sz(eds)) {
if(dsu.join(eds[i].first, eds[i].second)) {
tr[eds[i].first].pb(eds[i].second);
tr[eds[i].second].pb(eds[i].first);
}
}
dfs(1,1);
FOR(j,1,LG) FOR(i,1,N+1) {
par[i][j] = par[par[i][j-1]][j-1];
jmp[i][j] = min(jmp[i][j-1], jmp[par[i][j-1]][j-1]);
}
cin>>Q;
while(Q--) {
int s, t;
cin>>s>>t;
cout << get_ans(s, t) << nl;
}
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'void setIO(std::string)':
plan.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | 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... |