#include "Azer.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
const int maxn = 2020;
vector<pair<int, int>> g[maxn];
set<pair<int, int>> s;
void Send_d(int dist) {
for(int i = 0; i < 9; i++) {
SendA((dist >> i) & 1);
}
}
void Send_v(int v) {
for(int i = 0; i < 11; i++) {
SendA((v >> i) & 1);
}
}
int d[maxn], was[maxn];
int n, cnt, fnd, turn, last;
int cur_v, cur_d;
} // namespace
void InitA(int N, int A, std::vector<int> U, std::vector<int> V,
std::vector<int> C) {
n = N;
for(int i = 0; i < A; i++) {
g[U[i]].push_back({V[i], C[i]});
g[V[i]].push_back({U[i], C[i]});
}
for(int i = 0; i < n; i++) {
d[i] = 1e9;
}
d[0] = 0;
last = 0;
s.insert({0, 0});
turn = 9;
}
void ReceiveA(bool x) {
if(cnt < 9) {
cur_d += (x << cnt);
}
else {
cur_v += (x << (cnt - 9));
}
if(cnt == 8) {
cur_d += last;
if(!s.empty() && s.begin() -> first < cur_d) {
SendA(true);
Send_v(s.begin() -> second);
Send_d(s.begin() -> first - last);
}
else {
SendA(false);
turn = 20;
}
}
if(cnt % turn == turn - 1) {
fnd++;
int v = cur_v;
if(!s.empty() && s.begin() -> first < cur_d) {
v = s.begin() -> second;
}
s.erase({d[v], v});
if(turn == 20) {
s.erase({d[cur_v], cur_v});
d[cur_v] = min(d[cur_v], cur_d);
if(cur_v != v && !was[cur_v]) {
s.insert({d[cur_v], cur_v});
}
}
was[v] = 1;
for(auto [to, w] : g[v]) {
if(d[to] > d[v] + w) {
s.erase({d[to], to});
d[to] = d[v] + w;
s.insert({d[to], to});
}
}
last = d[v];
cur_v = cur_d = 0;
}
cnt++;
if(cnt == turn) {
cnt = 0;
turn = 9;
}
}
vector<int> Answer() {
return vector<int> (d, d + n);
}
#include "Baijan.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
const int maxn = 2020;
vector<pair<int, int>> g[maxn];
set<pair<int, int>> s;
void Send_d(int dist) {
for(int i = 0; i < 9; i++) {
SendB((dist >> i) & 1);
}
}
void Send_v(int v) {
for(int i = 0; i < 11; i++) {
SendB((v >> i) & 1);
}
}
int d[maxn], was[maxn];
int n, cnt, fnd, turn, last;
int cur_v, cur_d;
} // namespace
void InitB(int N, int B, std::vector<int> S, std::vector<int> T,
std::vector<int> D) {
n = N;
for(int i = 0; i < B; i++) {
g[S[i]].push_back({T[i], D[i]});
g[T[i]].push_back({S[i], D[i]});
}
for(int i = 0; i < n; i++) {
d[i] = 1e9;
}
d[0] = 0;
last = 0;
s.insert({0, 0});
turn = 1;
Send_d(0);
}
void ReceiveB(bool y) {
if(cnt == 0) {
if(y == 1) turn = 21;
else {
if(s.empty()) exit(0);
Send_v(s.begin() -> second);
cur_d = (1 << 20);
}
}
else {
if(cnt <= 11) {
cur_v += (y << (cnt - 1));
}
else {
cur_d += (y << (cnt - 12));
}
}
if(cnt % turn == turn - 1) {
fnd++;
int v = cur_v;
cur_d += last;
if(!s.empty() && s.begin() -> first <= cur_d) {
v = s.begin() -> second;
}
s.erase({d[v], v});
if(turn > 1) {
s.erase({d[cur_v], cur_v});
d[cur_v] = min(d[cur_v], cur_d);
if(cur_v != v && !was[cur_v]) {
s.insert({d[cur_v], cur_v});
}
}
was[v] = 1;
for(auto [to, w] : g[v]) {
if(d[to] > d[v] + w) {
s.erase({d[to], to});
d[to] = d[v] + w;
s.insert({d[to], to});
}
}
last = d[v];
cur_v = cur_d = 0;
if(fnd < n) {
if(!s.empty()) {
Send_d(s.begin() -> first - last);
}
else {
Send_d((1 << 20) - 1);
}
}
}
cnt++;
if(cnt == turn) {
cnt = 0;
turn = 1;
}
}