Submission #601436

# Submission time Handle Problem Language Result Execution time Memory
601436 2022-07-21T21:41:18 Z narvalo Ferries (NOI13_ferries) C++17
28 / 40
249 ms 30544 KB
#include <bits/stdc++.h>
 
using namespace std;

#define int long long
 
const int INF = (int)1e12;
 
int ferries() {
  int n , m;
  cin >> n >> m;
  vector<vector<pair<int , int>>> old_adj(n) , adj(n);
  vector<vector<int>> edges(n);
  for (int i = 0 ; i < m ; i++) {
    int a , b , c;
    cin >> a >> b >> c;
    a -= 1 , b -= 1;
    old_adj[a].emplace_back(b , c);
    edges[a].push_back(c);
  }
  for (int i = 0 ; i < n ; i += 1) {
    sort(edges[i].rbegin() , edges[i].rend());
  }
  vector<int> vis(n);
  function<bool(int)> Dfs = [&](int node) {
    if (vis[node] == 1) {
      return false;
    }
    else if (vis[node] == 2) {
      return true;
    }
    vis[node] = 1;
    for (auto x : old_adj[node]) {
      if (Dfs(x.first)) {
        adj[node].push_back(x);
      }
    }
    vis[node] = 2;
    return true;
  };
  for (int i = 0 ; i < n ; i++) {
    Dfs(i);
  }
  for (int i = 0 ; i < n ; i++) {
    for (int j = 0 ; j < (int)adj[i].size() ; j += 1) {
      adj[i][j].second = edges[i][j];
    }
  }
  vector<int> dp(n , -1);
  function<int(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();
    return dp[node] = res;
  };
  int res = Solve(0);
  return res;
}
 
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << ferries() << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 9 ms 2768 KB Output is correct
4 Correct 90 ms 24816 KB Output is correct
5 Correct 86 ms 24780 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 11 ms 2768 KB Output is correct
4 Correct 50 ms 12484 KB Output is correct
5 Correct 85 ms 19200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 3284 KB Output is correct
2 Correct 16 ms 3480 KB Output is correct
3 Correct 223 ms 30260 KB Output is correct
4 Correct 243 ms 28304 KB Output is correct
5 Correct 207 ms 28356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 249 ms 30544 KB Output isn't correct
2 Halted 0 ms 0 KB -