#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pp pop_back
#define pf push_front
#define pt pop_front
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pii pair<int,int>
#define vl vector<ll>
#define endl "\n"
#define fr(i,a,b) for(ll i=a; i<=b; i++)
#define fre(i,a,b) for(ll i=a; i>=b; i--)
#define kpnyh ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// __builtin_ctzll(bits) kalo mau iterasi bits 1 nya aja
const ll maxn = 2e5+7, maxn2 = 1e7+7, modn2 = 1e9+7, modn = 998244353;
using namespace std;
ll mul(ll x, ll y) {return (x*y) % modn;}
ll add(ll x, ll y) {return(x+y) % modn;}
ll dec(ll x, ll y) {return(x-y+modn) % modn;}
ll binpow(ll x, ll y) {ll ret = 1;while (y) {if (y&1) ret = mul(ret,x); y/=2;x = mul(x,x);}return ret;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
//inget kalo dapet ide tulis dulu langkah langkahnya biar ngga ada yang ke skip
//lu pernah debug sampe tengah malem buat ide yang udah bener cuman gegara ada yang ke skip
ll te,n,m,k,q;
priority_queue<pll, vector<pll>, greater<pll>>pq;
vector<vector<pll>>adj(maxn);
vector<vector<ll>>d;
void SP (ll start) {
vector<ll>dist(n+20,1e18);
dist[start] = 0;
pq.push({0,start});
while (!pq.empty()) {
ll cost = pq.top().fi, node = pq.top().se; pq.pop();
if (dist[node] < cost) continue;
for (auto [next,weight] : adj[node]) {
if (weight + cost < dist[next]) {
dist[next] = weight + cost;
pq.push({dist[next], next});
}
}
}
d.pb(dist);
}
void solve() {
cin>>k>>n>>m>>q;
for (ll i=1; i<=m; i++) {
ll u,v,w; cin>>u>>v>>w;
adj[u].pb({v,w});
}
map<ll,ll>mp;
ll now = 0;
while (q--) {
ll u,v; cin>>u>>v;
if (mp[u] == 0) {
now++;
mp[u] = now;
SP(u);
}
if (d[mp[u]-1][v] == 1e18) cout<<-1<<endl;
else cout<<d[mp[u] - 1][v]<<endl;
}
}
/*\
1
5
6 6 6 6 6
*/
int main () {
kpnyh
te=1;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// cin>>te;
while (te--) {
//cout<<endl;
solve();
}
}
| # | 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... |