Submission #601423

# Submission time Handle Problem Language Result Execution time Memory
601423 2022-07-21T20:50:07 Z narvalo Ferries (NOI13_ferries) C++17
0 / 40
219 ms 38868 KB
#include <bits/stdc++.h>
 
using namespace std;

#define int long long
 
const int INF = (int)1e12;
 
int ferries(int n , int m , vector<int> a , vector<int> b , vector<int> c) {
  vector<vector<pair<int , int>>> adj(n) , new_adj(n) , rev(n);
  for (int i = 0 ; i < m ; i++) {
    a[i] -= 1 , b[i]   -= 1;
    adj[a[i]].emplace_back(b[i] , c[i]);
    rev[b[i]].emplace_back(a[i] , c[i]);
  }
  auto Dijk = [&](int from , bool g) {
    vector<int> dest(n , INF);
    dest[from] = 0;
    #define pi pair<int , int>
    priority_queue<pi , vector<pi> , greater<pi>> pq;
    pq.emplace(dest[from] , from);
    while (!pq.empty()) {
      auto X = pq.top();
      pq.pop();
      if (X.first != dest[X.second]) continue;
      for (auto x : (g ? adj[X.second] : rev[X.second])) {
        if (dest[x.first] > X.first + x.second) {
          dest[x.first] = X.first + x.second;
        }
      }
    }
    return dest;
  };
  vector<int> dest = Dijk(n - 1 , 0);
  #define pi pair<int , int>
  priority_queue<pi , vector<pi> , greater<pi>> pq;
  vector<int> d(n , INF);
  d[0] = 0;
  pq.emplace(d[0] , 0);
  while (!pq.empty()) {
    auto X = pq.top();
    pq.pop();
    if (X.first != d[X.second]) continue;
    vector<int> c1;
    vector<pair<int , int>> c2;
    for (auto x : adj[X.second]) {
      c1.push_back(x.second);
      c2.emplace_back(dest[x.first] , x.first);
    }
    sort(c1.begin() , c1.end());
    sort(c2.begin() , c2.end());
    for (int i = 0 ; i < (int)c1.size() ; i += 1) {
      int go = (int)c1.size() - i - 1;
      int id = c2[go].second;
      if (d[id] > c1[i] + X.first) {
        d[id] = c1[i] + X.first;
        pq.emplace(d[id] , id);
      }
    }
  }
  int res = d[n - 1];
  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';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 10 ms 3960 KB Output is correct
4 Runtime error 110 ms 34276 KB Memory limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 596 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 4352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 219 ms 38868 KB Memory limit exceeded
2 Halted 0 ms 0 KB -