Submission #980537

#TimeUsernameProblemLanguageResultExecution timeMemory
980537hieptk경주 (Race) (IOI11_race)C++17
21 / 100
3067 ms20124 KiB
#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 (w == k) Res = 1; else if (res.count(k - w)) { Res = min(Res, res[k - w] + 1); } for (auto [cost, len]: resj) { if (cost + w > k) continue; 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"; // cout << Res << "\n"; return res; } void bfs(int i, int n, int k) { queue<int> q; q.push(i); vector<pair<ll, ll>> d(n, {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; if (d[j].first == k) { Res = min(Res, (int) d[j].second); return; } if (d[j].second < Res && d[j].first < k) q.push(j); } } } } 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); for (int i = 0; i < n; ++i) bfs(i, n, k); return Res <= n ? Res : -1; } // int main() { // #ifdef LOCAL // freopen("input.txt", "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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...