# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
601490 |
2022-07-22T05:47:08 Z |
narvalo |
Ferries (NOI13_ferries) |
C++17 |
|
413 ms |
29664 KB |
#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 time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
11 ms |
2636 KB |
Output is correct |
4 |
Correct |
103 ms |
23944 KB |
Output is correct |
5 |
Correct |
100 ms |
27028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
8 ms |
2640 KB |
Output is correct |
4 |
Correct |
45 ms |
12132 KB |
Output is correct |
5 |
Correct |
83 ms |
18380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
3156 KB |
Output is correct |
2 |
Correct |
20 ms |
3156 KB |
Output is correct |
3 |
Correct |
332 ms |
28548 KB |
Output is correct |
4 |
Correct |
370 ms |
28812 KB |
Output is correct |
5 |
Correct |
385 ms |
28824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
323 ms |
28528 KB |
Output is correct |
2 |
Correct |
336 ms |
28524 KB |
Output is correct |
3 |
Correct |
413 ms |
29664 KB |
Output is correct |
4 |
Correct |
401 ms |
29636 KB |
Output is correct |
5 |
Correct |
383 ms |
29628 KB |
Output is correct |