#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int N;
vector<pair<int, int> > adj[100001];
vector<int> dijkstra(int src)
{
vector<int> dist(N, 999999999);
dist[src] = 0;
priority_queue<pair<int, int> > pq;
pq.push(make_pair(0, src));
while(!pq.empty())
{
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
if(dist[here] < cost) continue;
for(int i=0; i<adj[here].size(); i++)
{
int there = adj[here][i].first;
int nextDist = cost + adj[here][i].second;
if(dist[there] > nextDist)
{
dist[there] = nextDist;
pq.push(make_pair(-nextDist, there));
}
}
}
return dist;
}
int main()
{
int M;
cin >> N >> M;
for(int i=0; i<M; i++)
{
int a, b, c = 1;
cin >> a >> b;
adj[a-1].push_back(make_pair(b-1, c));
}
vector<int> Dist = dijkstra(0);
if(N == 1) cout << "0";
else if(Dist[N-1] == 999999999) cout << "-1";
else cout << Dist[N-1];
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4020 KB |
Output is correct |
2 |
Correct |
0 ms |
4020 KB |
Output is correct |
3 |
Correct |
0 ms |
4020 KB |
Output is correct |
4 |
Correct |
0 ms |
4020 KB |
Output is correct |
5 |
Correct |
0 ms |
4020 KB |
Output is correct |
6 |
Correct |
0 ms |
4020 KB |
Output is correct |
7 |
Correct |
0 ms |
4020 KB |
Output is correct |
8 |
Correct |
0 ms |
4020 KB |
Output is correct |
9 |
Correct |
0 ms |
4020 KB |
Output is correct |
10 |
Correct |
60 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4412 KB |
Output is correct |
2 |
Correct |
100 ms |
7448 KB |
Output is correct |
3 |
Correct |
164 ms |
7580 KB |
Output is correct |
4 |
Correct |
132 ms |
7448 KB |
Output is correct |
5 |
Correct |
136 ms |
7580 KB |
Output is correct |
6 |
Correct |
104 ms |
7448 KB |
Output is correct |
7 |
Correct |
104 ms |
7448 KB |
Output is correct |
8 |
Correct |
220 ms |
8532 KB |
Output is correct |
9 |
Correct |
128 ms |
7448 KB |
Output is correct |
10 |
Correct |
208 ms |
7844 KB |
Output is correct |