제출 #557558

#제출 시각아이디문제언어결과실행 시간메모리
557558fatemetmhrToll (BOI17_toll)C++17
56 / 100
3082 ms18908 KiB
// `Be name khoda` // #include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define all(x) x.begin(), x.end() #define fi first #define se second const int maxn5 = 2e5 + 10; const int inf = 1e9; int dis[maxn5], prec[maxn5], no = 0, cmp[maxn5], h[maxn5]; vector <pair<int, int>> jda[maxn5], adj[maxn5]; bool mark[maxn5]; inline void dfs(int v){ mark[v] = true; cmp[v] = no; for(auto [u, t] : adj[v]){ h[u] = h[v] + t; dfs(u); } return; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k, n, m, q; cin >> k >> n >> m >> q; for(int i = 0; i < m; i++){ int a, b, t; cin >> a >> b >> t; jda[b].pb({a, t}); adj[a].pb({b, t}); } fill(prec, prec + n + 5, inf); prec[0] = 0; for(int i = 0; i < n; i++) for(auto [u, t] : adj[i]) prec[u] = min(prec[u], prec[i] + t); for(int i = 0; i < n; i++) if(prec[i] == inf) prec[i] = -1; for(int i = 0; i < n; i++) if(!mark[i] && k == 1){ dfs(i); no++; } for(int i = 0; i < q; i++){ int a, b; cin >> a >> b; if(a == 0){ cout << prec[b] << '\n'; continue; } if(k == 1){ if(cmp[a] == cmp[b]) cout << h[b] - h[a] << '\n'; else cout << -1 << '\n'; continue; } fill(dis, dis + n + 4, inf); dis[b] = 0; for(int i = b; i > a; i--){ for(auto [u, t] : jda[i]) dis[u] = min(dis[u], dis[i] + t); } if(dis[a] == inf) dis[a] = -1; cout << dis[a] << '\n'; } }
#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...