답안 #601362

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
601362 2022-07-21T18:31:17 Z narvalo 페리들 (NOI13_ferries) C++17
28 / 40
155 ms 30172 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int INF = (int)1e18;

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) , vis(n);
  function<long long(int)> Solve = [&](int node) -> long long {
    if (node == n - 1) {
      return 0;
    }
    if (vis[node] == 1) {
      return -1;
    }
    if (dp[node] != -1) {
      return dp[node];
    }
    vis[node] = 1;
    vector<int> d , s;
    for (auto x : adj[node]) {
      int g = Solve(x.first);
      if (g != -1) {
        d.emplace_back(g);
      }
      s.emplace_back(x.second);
    }
    sort(d.begin() , d.end());
    sort(s.begin() , s.end());
    auto Get = [&]() {
      int val = INF;
      for (int i = 0 ; i < (int)d.size() ; i += 1) {
        val = min(val , s[i] + d[(int)d.size() - i - 1]);
      }
      return val;
    };
    int res = Get();
    reverse(s.begin() , s.end());
    reverse(d.begin() , d.end());
    res = max(res , Get());
    vis[node] = 2;
    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<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 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 8 ms 2384 KB Output is correct
4 Correct 92 ms 21252 KB Output is correct
5 Correct 81 ms 21244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 7 ms 2308 KB Output is correct
4 Correct 35 ms 10500 KB Output is correct
5 Correct 54 ms 13192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 3088 KB Output is correct
2 Correct 11 ms 3092 KB Output is correct
3 Correct 155 ms 28480 KB Output is correct
4 Correct 136 ms 26056 KB Output is correct
5 Correct 124 ms 26056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 152 ms 30172 KB Output isn't correct
2 Halted 0 ms 0 KB -