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>
using namespace std;
const int inf = 1000000000;
int n, m, k, qn;
int adj[70][70];
int d[70], t[70];
priority_queue <pair<pair<int, int>, int> > q; //{{distance, time}, town}
int main()
{
cin>>n>>m;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
adj[i][j] = inf;
for(int i = 0; i < m; i++)
{
int a, b, t;
cin>>a>>b>>t;
a--;
b--;
adj[a][b] = min(adj[a][b], t);
}
cin>>k>>qn;
while(qn--)
{
int from, to;
cin>>from>>to;
from--;
to--;
for(int i = 0; i < n; i++)
d[i] = t[i] = inf;
q.push({{0, 0}, from});
while(q.size())
{
int dis = -q.top().first.first;
int tim = -q.top().first.second;
int a = q.top().second;
q.pop();
if(dis > k || t[a] <= tim)
continue;
d[a] = dis;
t[a] = tim;
if(dis == k)
continue;
for(int i = 0; i < n; i++)
if(t[a] + adj[a][i] < t[i])
q.push({{-(d[a] + 1), -(t[a] + adj[a][i])}, i});
}
/*
cout<<"d: ";
for(int i = 0; i < n; i++)
cout<<d[i]<<" ";
cout<<endl<<"t: ";
for(int i = 0; i < n; i++)
cout<<t[i]<<" ";
cout<<endl<<"r: ";
*/
if(t[to] == inf)
cout<<-1<<endl;
else
cout<<t[to]<<endl;
}
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... |