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;
#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';
}
# | 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... |