이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "Azer.h"
#include <bits/stdc++.h>
using namespace std;
namespace A {
const int MAXN = 2100;
const int BITS_IN_DISTANCES = 9;
const int BITS_IN_VERTICES = 11;
const int INF_DISTANCE = (1 << BITS_IN_DISTANCES) - 1;
int d[MAXN];
bool vis[MAXN];
int n;
vector<pair<int, int>> adj[MAXN];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
int longestCommonDistance = 0;
pair<int, int> nextShortest;
int distancesKnown = 0;
int distanceReceived = 0;
int vertexReceived = 0;
int receivedBitsCount = 0;
bool gettingDistance = true; // true if receiveA gets bits for distances, false if receiveA gets bits for vertex number
void sendNumber(int number, int bits) {
for (int i = 0; i < bits; i++) {
SendA(number & (1 << i));
}
}
void sendNextShortest() {
int distance = nextShortest.first - longestCommonDistance; // subtract to fit in BITS_IN_DISTANCES bytes
sendNumber(distance, BITS_IN_DISTANCES);
}
void cleanPq() {
while (!pq.empty() && vis[pq.top().second]) { // don't consider if already distance known (if vis[v] is true)
pq.pop();
}
}
void startSending() {
cleanPq();
int v = pq.top().second; // both people now know that the next shortest distance is to v
pq.pop();
longestCommonDistance = d[v];
vis[v] = true;
distancesKnown++;
if (distancesKnown == n) return; // we now know all distances
for (auto p : adj[v]) {
int u = p.first;
int w = p.second;
int newDist = d[v] + w;
if (d[u] == -1 || d[u] > newDist) {
d[u] = newDist;
pq.emplace(d[u], u);
}
}
cleanPq();
if (pq.empty()) nextShortest = {INF_DISTANCE + longestCommonDistance, n};
else nextShortest = pq.top();
sendNextShortest();
}
void distanceWasReceived() {
distanceReceived += longestCommonDistance; // because it was subtracted before sending
if (distanceReceived < nextShortest.first) { // in equality case, Azer will send vertex number
// we should now expect bits for the vertex number
gettingDistance = false;
} else {
// we have better distance, we will send the vertex number of it
gettingDistance = true;
distanceReceived = 0;
sendNumber(nextShortest.second, BITS_IN_VERTICES);
startSending();
}
}
void vertexWasReceived() {
if (d[vertexReceived] == -1 || distanceReceived < d[vertexReceived]) {
d[vertexReceived] = distanceReceived;
pq.emplace(distanceReceived, vertexReceived);
}
gettingDistance = true;
distanceReceived = 0;
vertexReceived = 0;
startSending();
}
void receiveBitForDistance(bool x) {
if (x) distanceReceived += (1 << receivedBitsCount);
receivedBitsCount++;
if (receivedBitsCount == BITS_IN_DISTANCES) { // distance was got
receivedBitsCount = 0;
distanceWasReceived();
}
}
void receiveBitForVertex(bool x) {
if (x) vertexReceived += (1 << receivedBitsCount);
receivedBitsCount++;
if (receivedBitsCount == BITS_IN_VERTICES) { // vertex was got
receivedBitsCount = 0;
vertexWasReceived();
}
}
void receiveBit(bool x) {
if (gettingDistance)
receiveBitForDistance(x);
else
receiveBitForVertex(x);
}
} // namespace
void InitA(int N, int a, std::vector<int> U, std::vector<int> V,
std::vector<int> C) {
A::n = N;
for (int i = 0; i < N; i++) {
A::d[i] = -1;
}
for (int i = 0; i < a; i++) {
A::adj[U[i]].emplace_back(V[i], C[i]);
A::adj[V[i]].emplace_back(U[i], C[i]);
}
A::d[0] = 0;
A::pq.emplace(0, 0);
A::startSending();
}
void ReceiveA(bool x) {
A::receiveBit(x);
}
std::vector<int> Answer() {
vector<int> ans(A::n);
for (int i = 0; i < A::n; i++) ans[i] = A::d[i];
return ans;
}
/*
7 1 5
0 5 10
5 4 10
4 1 10
1 3 10
3 2 10
6 0 10
*/
#include "Baijan.h"
#include <bits/stdc++.h>
using namespace std;
namespace B {
const int MAXN = 2100;
const int BITS_IN_DISTANCES = 9;
const int BITS_IN_VERTICES = 11;
const int INF_DISTANCE = (1 << BITS_IN_DISTANCES) - 1;
int d[MAXN];
bool vis[MAXN];
int n;
vector<pair<int, int>> adj[MAXN];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
int longestCommonDistance = 0;
pair<int, int> nextShortest;
int distancesKnown = 0;
int distanceReceived = 0;
int vertexReceived = 0;
int receivedBitsCount = 0;
bool gettingDistance = true; // true if receiveA gets bits for distances, false if receiveA gets bits for vertex number
void sendNumber(int number, int bits) {
for (int i = 0; i < bits; i++) {
SendB(number & (1 << i));
}
}
void sendNextShortest() {
int distance = nextShortest.first - longestCommonDistance; // subtract to fit in BITS_IN_DISTANCES bytes
sendNumber(distance, BITS_IN_DISTANCES);
}
void cleanPq() {
while (!pq.empty() && vis[pq.top().second]) { // don't consider if already distance known (if vis[v] is true)
pq.pop();
}
}
void startSending() {
cleanPq();
int v = pq.top().second; // both people now know that the next shortest distance is to v
pq.pop();
longestCommonDistance = d[v];
vis[v] = true;
distancesKnown++;
if (distancesKnown == n) return; // we now know all distances
for (auto p : adj[v]) {
int u = p.first;
int w = p.second;
int newDist = d[v] + w;
if (d[u] == -1 || d[u] > newDist) {
d[u] = newDist;
pq.emplace(d[u], u);
}
}
cleanPq();
if (pq.empty()) nextShortest = {INF_DISTANCE + longestCommonDistance, n};
else nextShortest = pq.top();
sendNextShortest();
}
void distanceWasReceived() {
distanceReceived += longestCommonDistance; // because it was subtracted before sending
if (distanceReceived <= nextShortest.first) { // in equality case, Azer will send vertex number
// we should now expect bits for the vertex number
gettingDistance = false;
} else {
// we have better distance, we will send the vertex number of it
gettingDistance = true;
distanceReceived = 0;
sendNumber(nextShortest.second, BITS_IN_VERTICES);
startSending();
}
}
void vertexWasReceived() {
if (d[vertexReceived] == -1 || distanceReceived < d[vertexReceived]) {
d[vertexReceived] = distanceReceived;
pq.emplace(distanceReceived, vertexReceived);
}
gettingDistance = true;
distanceReceived = 0;
vertexReceived = 0;
startSending();
}
void receiveBitForDistance(bool x) {
if (x) distanceReceived += (1 << receivedBitsCount);
receivedBitsCount++;
if (receivedBitsCount == BITS_IN_DISTANCES) { // distance was got
receivedBitsCount = 0;
distanceWasReceived();
}
}
void receiveBitForVertex(bool x) {
if (x) vertexReceived += (1 << receivedBitsCount);
receivedBitsCount++;
if (receivedBitsCount == BITS_IN_VERTICES) { // vertex was got
receivedBitsCount = 0;
vertexWasReceived();
}
}
void receiveBit(bool x) {
if (gettingDistance)
receiveBitForDistance(x);
else
receiveBitForVertex(x);
}
} // namespace
void InitB(int N, int b, std::vector<int> S, std::vector<int> T,
std::vector<int> D) {
B::n = N;
for (int i = 0; i < N; i++) {
B::d[i] = -1;
}
for (int i = 0; i < b; i++) {
B::adj[S[i]].emplace_back(T[i], D[i]);
B::adj[T[i]].emplace_back(S[i], D[i]);
}
B::d[0] = 0;
B::pq.emplace(0, 0);
B::startSending();
}
void ReceiveB(bool y) {
B::receiveBit(y);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |