Submission #1129475

#TimeUsernameProblemLanguageResultExecution timeMemory
1129475zhasynEvacuation plan (IZhO18_plan)C++20
54 / 100
4094 ms87064 KiB
#include <bits/stdc++.h> #define pb push_back #define pf push_front using namespace std; #define F first #define S second typedef long long ll; #define pii pair <int, int> #define pll pair <ll, ll> typedef long double ld; const ll N = 1e5 + 100, M = 500 + 10, len = 315, inf = 1e18; const ll mod = 998244353; ll um(ll a, ll b){ return (1LL * a * b) % mod; } ll subr(ll a, ll b){ return ((1LL * a - b) % mod + mod) % mod; } ll bp(ll x, ll step){ ll res = 1; while(step){ if(step & 1) res = um(res, x); x = um(x, x); step /= 2; } return res; } ll inv(ll x){ return bp(x, mod - 2); } int n, m, k, d[N]; vector <pii> q[N]; bool was[N]; void bfs(int from, int to){ set <pii> st; st.insert({-d[from], from}); for(int i = 1; i <= n; i++){ was[i] = false; } while((int)st.size() != 0){ int v = st.begin()->S, pred = st.begin()->F; was[v] = true; if(v == to){ cout << -pred << endl; } //cout << v << " "<< pred << endl; st.erase(st.begin()); for(auto u : q[v]){ if(was[u.F]) continue; st.insert({max(-d[u.F], pred), u.F}); } } } void bfs(int x){ set <pii> st; d[x] = 0; st.insert({d[x], x}); while((int)st.size() != 0){ int v = st.begin()->S; st.erase(st.begin()); for(auto u : q[v]){ if(d[u.F] > d[v] + u.S){ d[u.F] = d[v] + u.S; st.insert({d[u.F], u.F}); } } } } int main() { //ios_base::sync_with_stdio(false); //cin.tie(nullptr); //cout.tie(nullptr); cin >> n >> m; map <pii, bool> fr; for(int i = 0, a, b, c; i < m; i++){ cin >> a >> b >> c; q[a].pb({b, c}); q[b].pb({a, c}); fr[{a, b}] = fr[{b, a}] = true; } for(int i = 1; i <= n; i++){ d[i] = INT_MAX; } cin >> k; for(int i = 0, x; i < k; i++){ cin >> x; bfs(x); } // for(int i = 1; i <= n; i++){ // cout << i << " "<< d[i] << endl; // } int quer; cin >> quer; for(int i = 0, a, b; i < quer; i++){ cin >> a >> b; if(fr[{a, b}]) cout << min(d[a], d[b]) << endl; else bfs(a, b); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...