제출 #601423

#제출 시각아이디문제언어결과실행 시간메모리
601423narvalo페리들 (NOI13_ferries)C++17
0 / 40
219 ms38868 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...