이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = (int)1e12;
int ferries(int n , int m , vector<int> a , vector<int> b , vector<int> c) {
vector<vector<pair<int , int>>> adj(n) , new_adj(n) , rev(n);
for (int i = 0 ; i < m ; i++) {
a[i] -= 1 , b[i] -= 1;
adj[a[i]].emplace_back(b[i] , c[i]);
rev[b[i]].emplace_back(a[i] , c[i]);
}
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;
}
}
}
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);
int n , m;
cin >> n >> m;
vector<int> a(m) , b(m) , c(m);
for (int i = 0 ; i < m ; i += 1) {
cin >> a[i] >> b[i] >> c[i];
}
cout << ferries(n , m , a , b , c) << '\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... |