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 <cstdio>
#include <vector>
#include <queue>
using namespace std;
vector<int > G[100001];
int n,m;
int dijkstra(int s,int t)
{
priority_queue<pair<int,int> > pq;
pq.push(make_pair(0,s));
vector<int> dist(n + 1, 987654321);
dist[s] = 0;
while(!pq.empty())
{
int v = pq.top().second,cost = -pq.top().first;
pq.pop();
if(dist[v] < cost)continue;
for(int i = 0; i < G[v].size(); i++)
{
int u = G[v][i], nextcost = cost + 1;
if(dist[u] > nextcost)
{
dist[u] = nextcost;
if(u == t)return nextcost;
pq.push(make_pair(-nextcost,u));
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
}
printf("%d\n",dijkstra(1,n));
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |