# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
715326 | lam | Highway Tolls (IOI18_highway) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 has=0;
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;
}
#include "highway.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e5 + 10;
typedef pair<int,int> ii;
#define ff first
#define ss second
int n,m;
ll dist,w1,w2,dist2;
vector <ii> adj[maxn];
ii e[maxn];
int d[maxn]; ii par[maxn];
int side[maxn];
int dnc()
{
int l=0; int r=m-1;
while (l<r)
{
int mid=(l+r)/2;
vector <int> dau(m,0);
for (int i=0; i<=mid; i++) dau[i] = 1;
ll temp = ask(dau);
if (temp > dist) r = mid;
else l=mid+1;
}
return r;
}
int trace(int lx, int rx, vector <int>&nodes, vector <int32_t> &dau)
{
if (lx==rx) return nodes[lx];
int mid=(lx+rx)/2;
for (int i=lx; i<=mid; i++) dau[par[nodes[i]].ss] = 1;
bool check = (ask(dau) > dist);
for (int i=lx; i<=mid; i++) dau[par[nodes[i]].ss] = 0;
if (check) return trace(lx,mid,nodes,dau);
else return trace(mid+1,rx,nodes,dau);
}
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
m=U.size(); n=N; w1=A; w2=B;
fill_n(side,n,0);
for (int i=0; i<n; i++) par[i]={-1,-1};
cerr<<":>"<<endl;
for (int i=0; i<n; i++) adj[i].clear();
for (int i=0; i<m; i++) e[i].ff=U[i], e[i].ss = V[i], adj[e[i].ff].push_back({e[i].ss,i}), adj[e[i].ss].push_back({e[i].ff,i});
vector <int32_t> dau(m,0);
dist=ask(dau);
dist2=ask(dau);
cerr<<dist<<endl;
int idx = dnc();
cerr<<idx<<endl;
fill_n(d,n,-1);
int u=e[idx].ff; int v=e[idx].ss;
d[u]=d[v]=0; par[u] = par[v] = {-1,-1};
dau[idx] = -1;
queue<int> q;
q.push(u); q.push(v);
side[u] = 1; side[v] = 2;
while (!q.empty())
{
int u=q.front(); q.pop();
for (ii i:adj[u])
{
int v=i.ff; int id = i.ss;
if (d[v]==-1)
{
par[v] = {u,id};
side[v] = side[u];
d[v] = d[u]+1;
// cerr<<u<<" - "<<v<<endl;
dau[id] = side[v];
q.push(v);
}
}
}
vector <int32_t> tmp(m,0);
for (int i=0; i<m; i++) if (dau[i]==0) tmp[i]=1;
for (int i=0; i<m; i++) if (dau[i]==1) tmp[i]=1;
// for (int i:tmp) cerr<<i<<' '; cerr<<endl;cerr<<" = "<<ask(tmp)<<' '<<dist<<endl;
ll du = 1LL*(ask(tmp)-dist)/(w2-w1);
for (int i=0; i<m; i++) if (dau[i]==1) tmp[i]=0;
for (int i=0; i<m; i++) if (dau[i]==2) tmp[i]=1;
ll dv = 1LL*(ask(tmp)-dist)/(w2-w1);
for (int i=0; i<m; i++) if (dau[i]==2) tmp[i]=0;
// cerr<<u<<' '<<du<<" & "<<v<<' '<<dv<<endl;
vector <int> nodes;
for (int i=0; i<n; i++) if (side[i]==1&&d[i]==du) nodes.push_back(i);
// for (int i=0; i<n; i++) cerr<<d[i]<<','<<side[i]<<' '; cerr<<endl;
int s,t;
// cerr<<nodes.size()<<endl;
if (du==0) s=u;
else s=trace(0,nodes.size()-1,nodes,tmp);
nodes.clear();
for (int i=0; i<n; i++) if (side[i]==2&&d[i]==dv) nodes.push_back(i);
if (dv==0) t=v;
else t=trace(0,nodes.size()-1,nodes,tmp);
answer(s,t);
}
int main() {
N = read_int();
M = read_int();
A = read_int();
B = read_int();
S = read_int();
T = 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});
}
cerr<<":>>"<<endl;
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;
}
/**
6 15 1 2 3 5
0 1
0 2
0 3
0 4
0 5
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
**/