#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
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> dp(n , -1);
function<int(int)> Solve = [&](int node) {
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;
}
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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
9 ms |
1580 KB |
Output is correct |
4 |
Correct |
102 ms |
12900 KB |
Output is correct |
5 |
Correct |
75 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
1492 KB |
Output is correct |
4 |
Correct |
42 ms |
6584 KB |
Output is correct |
5 |
Correct |
49 ms |
9096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1960 KB |
Output is correct |
2 |
Correct |
11 ms |
2388 KB |
Output is correct |
3 |
Correct |
134 ms |
21800 KB |
Output is correct |
4 |
Correct |
128 ms |
19832 KB |
Output is correct |
5 |
Correct |
138 ms |
19076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
126 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |