Submission #601327

#TimeUsernameProblemLanguageResultExecution timeMemory
601327narvaloFerries (NOI13_ferries)C++17
17 / 40
106 ms22884 KiB
#include <bits/stdc++.h> using namespace std; int ferries(int n , int m , vector<int> a , vector<int> b , vector<int> c) { vector<vector<pair<int , int>>> adj(n); for (int i = 0 ; i < m ; i++) { a[i] -= 1 , b[i] -= 1; adj[a[i]].emplace_back(b[i] , c[i]); } vector<int> s , d; bool sub1 = ((int)s.size() == (int)d.size() && (int)s.size() == n - 2); bool sub2 = true; for (int i = 1 ; i < n - 1 ; i += 1) { sub2 |= ((int)adj[i].size() == 1); } vector<int> mx(n); for (int i = 0 ; i < n ; i++) { for (auto x : adj[i]) { mx[i] = max(mx[i] , x.second); } } if (sub1 || sub2) { if (sub1) { for (int i = 0 ; i < (int)adj[0].size() ; i += 1) { s.push_back(adj[0][i].second); } sort(s.begin() , s.end()); for (int i = 1 ; i < n - 1 ; i += 1) { d.push_back(adj[i][0].second); } sort(d.begin() , d.end()); } else { vector<int> vis(n); vis[0] = 1; vector<int> dp(n , -1); function<int(int)> Dfs = [&](int node) { if (node == n - 1) { return dp[node] = 0; } if (vis[node] == 1) { return -1; } else if (vis[node] == 2) { return dp[node]; } vis[node] = 1; int val = 0; for (auto x : adj[node]) { val = Dfs(x.first); if (val != -1) { val += x.second; } } vis[node] = 2; return dp[node] = val; }; // maximize the minimum for (int i = 0 ; i < (int)adj[0].size() ; i++) { int cur = Dfs(adj[0][i].first); if (cur > -1) { s.push_back(cur); } d.push_back(adj[0][i].second); } sort(s.begin() , s.end()); sort(d.begin() , d.end()); } auto Get = [&]() { int val = (int)1e9; for (int i = 0 ; i < (int)s.size() ; i += 1) { val = min(val , s[i] + d[(int)s.size() - 1 - i]); } return val; }; int res = Get(); reverse(s.begin() , s.end()); reverse(d.begin() , d.end()); res = max(res , Get()); return res; } else { const int INF = (int)1e9; vector<int> dest(n , INF); #define pi pair<int , int> priority_queue<pi , vector<pi> , greater<pi>> pq; dest[0] = 0; pq.emplace(dest[0] , 0); while (!pq.empty()) { auto X = pq.top(); pq.pop(); if (X.first != dest[X.second]) continue; for (auto x : adj[X.second]) { if (dest[x.first] > X.first + mx[X.second]) { dest[x.first] = X.first + mx[X.second]; pq.emplace(dest[x.first] , x.first); } } } int res = dest[n - 1]; return res; } } int 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...