Submission #628072

#TimeUsernameProblemLanguageResultExecution timeMemory
628072ntttToll (BOI17_toll)C++14
18 / 100
3070 ms6588 KiB
#include<bits/stdc++.h> using namespace std; #define MASK(i) (1LL << (i)) #define BIT(x, i) (((x) >> (i)) & 1) #define fi first #define se second #define ll long long #define mp make_pair #define pb push_back #define pii pair<ll, int> #define all(x) (x).begin(), (x).end() #define task "name" const int oo = 1e9 + 7; const ll loo = (ll)1e18 + 7; const int MOD = 1e9 + 7; const int N = 5e4 + 3; const int M = 1e4 + 9; const int BASE = 10; template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;} template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;} struct query { int u, v, id; bool operator < (const query &a) const { return u < a.u; } } que[M]; int k, n, m, q; vector<pii> adj[N]; ll d[N]; ll ans[M]; void dijkstra(int s) { memset(d, 0x3f, sizeof d); priority_queue<pii, vector<pii>, greater<pii>> kew; kew.push(mp(0, s)); d[s] = 0; while(!kew.empty()) { int u = kew.top().se; ll du = kew.top().fi; kew.pop(); for(pii i : adj[u]) { int v = i.se; ll uv = i.fi; if(minimize(d[v], du + uv)) { kew.push(mp(d[v], v)); } } } } void Solve() { cin >> k >> n >> m >> q; for(int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; adj[u].pb(mp(w, v)); } for(int i = 1; i <= q; i++) { int u, v; cin >> u >> v; que[i] = {u, v, i}; } sort(que + 1, que + 1 + q); int s = -1; for(int i = 1; i <= q; i++) { if(que[i].u != s) { s = que[i].u; dijkstra(s); } ans[que[i].id] = d[que[i].v]; } for(int i = 1; i <= q; i++) { if(ans[i] < loo) cout << ans[i] << '\n'; else cout << -1 << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen(task".inp", "r", stdin); // freopen(task".out", "w", stdout); int T = 1; //cin >> T; while(T--) { Solve(); } 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...