제출 #601487

#제출 시각아이디문제언어결과실행 시간메모리
601487narvalo페리들 (NOI13_ferries)C++17
33 / 40
477 ms33988 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int INF = (int)1e18; int ferries(int n , int m , vector<vector<pair<int , int>>> adj) { vector<int> dp(n , -1); function<long long(int)> Solve = [&](int node) -> long long { if (node == n - 1) { return 0; } if (dp[node] != -1) { return dp[node]; } vector<int> d; for (auto x : adj[node]) { d.push_back(Solve(x.first)); } sort(d.begin() , d.end()); vector<int> s; for (int i = 0 ; i < (int)adj[node].size() ; i += 1) { s.push_back(adj[node][i].second); } sort(s.begin() , s.end()); auto Get = [&]() { int val = INF; for (int i = 0 ; i < (int)s.size() ; i += 1) { val = min(val , s[i] + d[(int)s.size() - i - 1]); } return val; }; int res = Get(); reverse(s.begin() , s.end()); reverse(d.begin() , d.end()); res = max(res , Get()); return dp[node] = res; }; int res = Solve(0); return res; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n , m; cin >> n >> m; vector<vector<pair<int , int>>> adj(n) , rev(n); vector<vector<int>> edges(n); for (int i = 0 ; i < m ; i += 1) { int a , b , c; cin >> a >> b >> c; a -= 1 , b -= 1; edges[a].push_back(c); adj[a].emplace_back(b , c); rev[b].emplace_back(a , c); } bool ok = true; for (int i = 1 ; i < n - 1 ; i += 1) { ok &= ((int)adj[i].size() == 1); } if (ok) { int res = ferries(n , m , adj); cout << res << '\n'; return 0; } for (int i = 0 ; i < n ; i += 1) { sort(edges[i].begin() , edges[i].end()); } #define pi pair<int , int> priority_queue<pi , vector<pi> , greater<pi>> pq; vector<int> d(n , INF); d[n - 1] = 0; pq.emplace(d[n - 1] , n - 1); while (!pq.empty()) { auto t = pq.top(); pq.pop(); if (d[t.second] != t.first) continue; for (int i = 0 ; i < (int)rev[t.second].size() ; i += 1) { auto node = rev[t.second][i]; vector<pair<int , int>> c2; for (auto x : adj[node.first]) { c2.emplace_back(d[x.first] , x.first); } sort(c2.begin() , c2.end()); for (int j = 0 ; j < (int)edges[node.first].size() ; j++) { int new_id = (int)edges[node.first].size() - j - 1; int id = c2[new_id].second; if (id == t.second) { if (d[node.first] > t.first + edges[node.first][j]) { d[node.first] = t.first + edges[node.first][j]; pq.emplace(d[node.first] , node.first); } break; } else continue; } } } cout << d[0] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...