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>
typedef long long ll;
const ll mod = (ll) 1e9 + 7;
const ll mx = (ll) 1e5 +7;
using namespace std;
vector<vector<pair<ll,ll>>>v(50007);
ll k,n,q,m,ans=0;
ll vis[50007];
void dfs(ll idx,ll cnt,ll e)
{
vis[idx]=1;
if(idx==e)
{
ans=min(cnt,ans);
return;
}
for(int i=0;i<v[idx].size();i++)
{
if(vis[v[idx][i].first]==0)
dfs(v[idx][i].first,cnt+v[idx][i].second,e);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>k>>n>>m>>q;
memset(vis,0,sizeof vis);
for(int i=0;i<m;i++)
{
ll a,b,c;
cin>>a>>b>>c;
v[a].push_back({b,c});
}
for(int i=0;i<q;i++)
{
ll a,b;
ans=INT_MAX;
cin>>a>>b;
if(b>a)
{
cout<<-1<<endl;
continue;
}
dfs(a,0,b);
if(ans==INT_MAX)ans=-1;
cout<<ans<<endl;
}
}
Compilation message (stderr)
toll.cpp: In function 'void dfs(ll, ll, ll)':
toll.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i=0;i<v[idx].size();i++)
| ~^~~~~~~~~~~~~~
# | 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... |