제출 #775421

#제출 시각아이디문제언어결과실행 시간메모리
775421Alihan_8통행료 (IOI18_highway)C++17
5 / 100
109 ms16776 KiB
#ifndef EVAL #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <utility> #include <vector> //#include "highway.h" namespace { constexpr int MAX_NUM_CALLS = 100; constexpr long long INF = 1LL << 61; int N, M, A, B, S, T; std::vector<int> U, V; std::vector<std::vector<std::pair<int, int>>> graph; bool answered, wrong_pair; int num_calls; int read_int() { int x; if (scanf("%d", &x) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } return x; } void wrong_answer(const char *MSG) { printf("Wrong Answer: %s\n", MSG); exit(0); } } // namespace long long ask(const std::vector<int> &w) { if (++num_calls > MAX_NUM_CALLS) { wrong_answer("more than 100 calls to ask"); } if (w.size() != (size_t)M) { wrong_answer("w is invalid"); } for (size_t i = 0; i < w.size(); ++i) { if (!(w[i] == 0 || w[i] == 1)) { wrong_answer("w is invalid"); } } std::vector<bool> visited(N, false); std::vector<long long> current_dist(N, INF); std::queue<int> qa, qb; qa.push(S); current_dist[S] = 0; while (!qa.empty() || !qb.empty()) { int v; if (qb.empty() || (!qa.empty() && current_dist[qa.front()] <= current_dist[qb.front()])) { v = qa.front(); qa.pop(); } else { v = qb.front(); qb.pop(); } if (visited[v]) { continue; } visited[v] = true; long long d = current_dist[v]; if (v == T) { return d; } for (auto e : graph[v]) { int vv = e.first; int ei = e.second; if (!visited[vv]) { if (w[ei] == 0) { if (current_dist[vv] > d + A) { current_dist[vv] = d + A; qa.push(vv); } } else { if (current_dist[vv] > d + B) { current_dist[vv] = d + B; qb.push(vv); } } } } } return -1; } void answer(int s, int t) { if (answered) { wrong_answer("answered not exactly once"); } if (!((s == S && t == T) || (s == T && t == S))) { wrong_pair = true; } answered = true; } #endif // EVAL #include <bits/stdc++.h> #include "highway.h" using namespace std; #define all(x) x.begin(), x.end() #define pb push_back #define ln '\n' //#define int long long template <class _T> bool chmin(_T &x, const _T &y){ bool flag = false; if ( x > y ){ x = y; flag |= true; } return flag; } template <class _T> bool chmax(_T &x, const _T &y){ bool flag = false; if ( x < y ){ x = y; flag |= true; } return flag; } void find_pair(int n, vector<int> U, vector<int> V, int A, int B) { int m = (int)U.size(); assert(m == n - 1); vector <pair<int,int>> g[n], edge(m); vector <int> tree; for ( int i = 0; i < m; i++ ){ g[U[i]].pb({V[i], i}); g[V[i]].pb({U[i], i}); } function <void(int,int)> dfs = [&](int x, int par){ for ( auto [to, id]: g[x] ){ if ( to == par ){ continue; } tree.pb(id); edge[id] = {x, to}; dfs(to, x); } }; int dis = ask(vector <int> (m, 0)) / A * B; auto get = [&](int root){ tree.clear(); dfs(root, -1); int l = 0, r = m - 1; while ( l < r ){ int md = (l + r) >> 1; vector <int> f(m, 0); for ( int i = 0; i <= md; i++ ){ f[tree[i]] = 1; } if ( ask(f) == dis ) r = md; else l = md + 1; } return edge[tree[l]].second; }; int s = get(0), t = get(s); answer(s, t); } #ifndef EVAL /* 4 4 1 3 0 1 0 2 0 3 1 2 1 3 7 6 1 2 0 1 0 2 1 3 1 4 2 5 4 6 3 2 */ int main() { N = read_int(); M = read_int(); A = read_int(); B = read_int(); U.resize(M); V.resize(M); graph.assign(N, std::vector<std::pair<int, int>>()); for (int i = 0; i < M; ++i) { U[i] = read_int(); V[i] = read_int(); graph[U[i]].push_back({V[i], i}); graph[V[i]].push_back({U[i], i}); } S = read_int(); T = read_int(); answered = false; wrong_pair = false; num_calls = 0; find_pair(N, U, V, A, B); if (!answered) { wrong_answer("answered not exactly once"); } if (wrong_pair) { wrong_answer("{s, t} is wrong"); } printf("Accepted: %d\n", num_calls); return 0; } #endif // EVAL
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...