Submission #878118

#TimeUsernameProblemLanguageResultExecution timeMemory
878118arashMLGToll (BOI17_toll)C++17
100 / 100
870 ms5852 KiB
#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 <= min(en[b],m-1); 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;
	fill(beg,beg+N,-1); fill(en,en+N,-1);
	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 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...