Submission #980464

# Submission time Handle Problem Language Result Execution time Memory
980464 2024-05-12T07:45:22 Z hieptk Race (IOI11_race) C++17
0 / 100
3 ms 11096 KB
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include <map>
#include <sstream>
#include <stack>
#include <bitset>
 
using ll = long long;
using ld = long double;
 
using namespace std;

const int nm = 200002;
const ll inf = 1e18;

vector<pair<int, ll>> adj[nm];
int H[nm][2], L[nm];
int Res;

unordered_map<ll, int> dfs(int i, int p, int k) {
    unordered_map<ll, int> res;
    for (auto [j, w]: adj[i]) {
        if (j == p) continue;
        auto resj = dfs(j, i, k);
        for (auto [cost, len]: resj) {
            if (cost + w > k) continue;
            if (cost + w == k) {
                Res = min(Res, len + 1);
            } else if (res.count(k - cost - w)) {
                Res = min(Res, res[k - cost - w] + len + 1);
            }
            if (res.count(cost + w)) {
              res[cost + w] = min(res[cost + w], len + 1);
            } else {
              res[cost + w] = len + 1;
            }
        }
        res[w] = 1;
    }
    // cout << "dfs " << i << "\n";
    // for (auto [cost, len]: res) cout << cost << " " << len << "\n";
    return res;
}

void bfs(int i, int n, int k) {
  queue<int> q;
  q.push(i);
  vector<pair<ll, ll>> d(n);
  for (int j = 0; j < n; ++j) d[j] = {inf, inf};
  d[i] = {0, 0};
  while (q.size()) {
    int i = q.front();
    q.pop();
    for (auto [j, w]: adj[i]) {
      if (d[j].second > d[i].second + 1) {
        d[j].first = d[i].first + w;
        d[j].second = d[i].second + 1;
        q.push(j);
      }
    }
  }
  for (int j = 0; j < n; ++j) {
    if (d[j].first == k) Res = min(Res, (int) d[j].second);
  }
}

int best_path(int n, int k, int H[][2], int L[]) {
    for (int i = 0; i < n - 1; ++i) {
        adj[H[i][0]].emplace_back(H[i][1], L[i]);
        adj[H[i][1]].emplace_back(H[i][0], L[i]);
    }
    
    Res = n + 1;
    dfs(0, -1, k);
    return Res <= n ? Res : -1;
}
 
// int main() {
//     #ifdef LOCAL
//     freopen("grader.in.1", "r", stdin);
//     #endif
//     ios::sync_with_stdio(0);
//     cin.tie(0);
//     int n, k;
//     cin >> n >> k;
//     for (int i = 0; i < n - 1; ++i) cin >> H[i][0] >> H[i][1] >> L[i];
//     cout << best_path(n, k, H, L) << "\n";
// }
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 11096 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 11096 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 11096 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 11096 KB Output isn't correct
2 Halted 0 ms 0 KB -