#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = (int)1e12;
int ferries() {
int n , m;
cin >> n >> m;
vector<vector<pair<int , int>>> adj(n) , rev(n);
for (int i = 0 ; i < m ; i++) {
int a , b , c;
cin >> a >> b >> c;
a -= 1 , b -= 1;
adj[a].emplace_back(b , c);
rev[b].emplace_back(a , c);
}
auto Dijk = [&](int from , bool g) {
vector<int> dest(n , INF);
dest[from] = 0;
#define pi pair<int , int>
priority_queue<pi , vector<pi> , greater<pi>> pq;
pq.emplace(dest[from] , from);
while (!pq.empty()) {
auto X = pq.top();
pq.pop();
if (X.first != dest[X.second]) continue;
for (auto x : (g ? adj[X.second] : rev[X.second])) {
if (dest[x.first] > X.first + x.second) {
dest[x.first] = X.first + x.second;
pq.emplace(dest[x.first] , x.first);
}
}
}
return dest;
};
vector<int> dest = Dijk(n - 1 , 0);
#define pi pair<int , int>
priority_queue<pi , vector<pi> , greater<pi>> pq;
vector<int> d(n , INF);
d[0] = 0;
pq.emplace(d[0] , 0);
while (!pq.empty()) {
auto X = pq.top();
pq.pop();
if (X.first != d[X.second]) continue;
vector<int> c1;
vector<pair<int , int>> c2;
for (auto x : adj[X.second]) {
c1.push_back(x.second);
c2.emplace_back(dest[x.first] , x.first);
}
sort(c1.begin() , c1.end());
sort(c2.begin() , c2.end());
for (int i = 0 ; i < (int)c1.size() ; i += 1) {
int go = (int)c1.size() - i - 1;
int id = c2[go].second;
if (d[id] > c1[i] + X.first) {
d[id] = c1[i] + X.first;
pq.emplace(d[id] , id);
}
}
}
int res = d[n - 1];
return res;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << ferries() << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
10 ms |
2608 KB |
Output is correct |
4 |
Correct |
134 ms |
22228 KB |
Output is correct |
5 |
Correct |
130 ms |
22640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
10 ms |
2744 KB |
Output is correct |
4 |
Correct |
59 ms |
11728 KB |
Output is correct |
5 |
Correct |
101 ms |
14476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
2516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
243 ms |
22140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |