This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// `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];
vector <pair<int, int>> jda[maxn5], adj[maxn5];
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 < q; i++){
int a, b; cin >> a >> b;
if(a == 0){
cout << prec[b] << '\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 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... |