Submission #600796

# Submission time Handle Problem Language Result Execution time Memory
600796 2022-07-21T07:51:33 Z narvalo Ferries (NOI13_ferries) C++17
7 / 40
169 ms 29440 KB
#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;
  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());
  if ((int)s.size() == (int)d.size() && (int)s.size() == n - 2) {
    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 {
    vector<int> suf(n + 1);
    for (int i = n - 1 ; i >= 1 ; i -= 1) {
      suf[i] = adj[i][0].second;
      suf[i] += suf[i + 1];
    }
    // maximize the minimum
    sort(adj[0].begin() , adj[0].end());
    int res = (int)1e9;
    for (int i = 0 ; i < (int)adj[0].size() ; i += 1) {
      res = min(res , adj[0][i].second + suf[adj[0][i].first]);
    }
    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 time Memory Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 8 ms 1640 KB Output is correct
4 Correct 77 ms 12620 KB Output is correct
5 Correct 79 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 3316 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 169 ms 29440 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -