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 <stdio.h>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int p;
int minn;
int situ_count, goal_num;
queue<int> que;
map<int, bool> is_visited_map;
vector<vector<int> > graph;
int shortest_count;
vector<int> dijkstra(int v, int src){
vector<int> dist(v+1,987654321);
dist[src] = 0;
priority_queue<pair<int,int> > p_que;
p_que.push(make_pair(0,src));
while(!p_que.empty()){
int cost = -p_que.top().first;
int here = p_que.top().second;
p_que.pop();
if(cost > dist[here])
{
continue;
}
for(int i=1;i<graph[here].size();i++)
{
int there = graph[here][i];
int nextDist = cost + 1;
if(dist[there] > nextDist)
{
dist[there] = nextDist;
p_que.push(make_pair(-nextDist,there));
}
}
}
return dist;
}
int main(void){
scanf("%d%d", &goal_num, &situ_count);
graph.resize(goal_num+1);
int tmpa, tmpb;
for(int i=0; i<situ_count; i++){
scanf("%d%d", &tmpa, &tmpb);
graph[tmpa].push_back(tmpb);
}
vector<int> dist_vec = dijkstra(goal_num, 1);
if(dist_vec[goal_num] == 987654321){
printf("-1\n");
}
else{
printf("%d\n", dist_vec[goal_num]);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |