#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define LongDepTrai "toll"
#define ll long long
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii,ii>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
#define order_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
inline ll add(ll a, ll b, ll mod){ a += b; if(a >= mod) a -= mod; return a; }
inline ll sub(ll a, ll b, ll mod){ a -= b; if(a < 0) a += mod; return a; }
inline ll mul(ll a, ll b, ll mod){ return ( (ll)a * b ) % mod; }
static mt19937_64 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());
const int N=5e4+9;
int n,o,k,m,t,f[N],vs[N];
vii g[N];
vector<int> toco;
void dfs(int u){
vs[u]=1;
for(ii v:g[u]){
if(!vs[v.fi]) dfs(v.fi);
}
vs[u]=2;
toco.pb(u);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen(LongDepTrai".inp","r")){
freopen(LongDepTrai".inp","r",stdin);
freopen(LongDepTrai".out","w",stdout);
}
cin>>k>>n>>m>>o;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
g[u].pb({v,w});
}
for(int i=1;i<=n;i++){
f[i]=2e9;
}
for(int i=1;i<=o;i++){
int u,v;
cin>>u>>v;
toco.clear();
for(int j=1;j<=n;j++){
vs[j]=0;
f[j]=2e9;
}
dfs(u);
f[u]=0;
reverse(toco.begin(),toco.end());
for(int x:toco){
// cout<<x<<" ";
for(ii l:g[x]){
f[l.fi]=min(f[l.fi],f[x]+l.se);
}
}
if(f[v]==2e9) cout<<-1<<"\n";
else
cout<<f[v]<<"\n";
}
return 0;
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen(LongDepTrai".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen(LongDepTrai".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |