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>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 5e4 + 23;
const int inf = 2e9;
#define F first
#define S second
#define pb push_back
#define kill(x) cout<<x<<endl, exit(0);
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lc (v << 1)
#define rc ((v<<1) |1)
int n,m,q,k;
int beg[N],en[N];
vector<pair<pii,int>> edges;
int dist[N];
map<pii,int> mp;
int get(int a,int b) {
if(a > b) return -1;
if(a ==b) return 0;
if(beg[a] == -1 || en[b] == -1) return -1;
if(mp[{a,b}] != 0) return mp[{a,b}];
fill(dist+a+1, dist+b+1,inf);
dist[a] = 0;
for(int i = beg[a]; i <= en[b]; i ++) {
auto [e,w] = edges[i];
dist[e.S] = min(dist[e.S],dist[e.F] +w);
}
mp[{a,b}] = (dist[b] >= inf ? -1 : dist[b]);
return mp[{a,b}];
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>k>>n>>m>>q;
for(int i = 0 ;i < m ;i ++) {
int x,y,w;cin>>x>>y>>w;
edges.pb({{x,y},w});
}
sort(all(edges));
int i = 0;
for(auto [e,_] : edges) {
auto [x,y] = e;
if(beg[x] == -1) beg[x] =i;
en[y] = i;
i++;
}
while(q--) {
int a,b; cin>>a>>b;
cout<< get(a,b) << '\n';
}
return 0;
}
# | 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... |