#include "Azer.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
typedef long long llong;
const int DIST_BITS = 9;
const int NODE_BITS = 11;
const int MAXN = 2000 + 10;
const int MAXM = 500000 + 10;
const int INF = 1e9;
static int n, m;
static int lastDist;
static int dist[MAXN];
static bool vis[MAXN];
struct PQelement
{
int node;
int currDist;
friend bool operator < (const PQelement &a, const PQelement &b)
{
return a.currDist > b.currDist || (a.currDist == b.currDist && a.node > b.node);
}
};
static std::vector <std::pair <int,int>> g[MAXN];
static std::priority_queue <PQelement> pq;
static std::queue <int> receivedQueue;
int readA(int);
void writeA(int, int);
std::pair <int,int> readPair()
{
if (receivedQueue.front() == 1)
{
receivedQueue.pop();
return {n, 1e9};
}
receivedQueue.pop();
return {readA(NODE_BITS), readA(DIST_BITS)};
}
void receivePairA()
{
auto [otherNode, otherDist] = readPair();
// std::cout << "receive from B: " << otherNode << ' ' << otherDist << '\n';
otherDist += lastDist;
if (otherNode < n) pq.push({otherNode, otherDist});
while (pq.size() && vis[pq.top().node])
{
pq.pop();
}
if (pq.size())
{
// std::cout << "hereAAA: " << pq.top().currDist << ' ' << lastDist << '\n';
// std::cout << "topA: " << pq.top().node << ' ' << pq.top().currDist << ' ' << lastDist << '\n';
auto [node, currDist] = pq.top();
pq.pop();
vis[node] = true;
dist[node] = currDist;
lastDist = currDist;
for (const auto &[u, edge] : g[node])
{
if (dist[u] > dist[node] + edge)
{
dist[u] = dist[node] + edge;
pq.push({u, dist[u]});
}
}
}
while (pq.size() && vis[pq.top().node])
{
pq.pop();
}
if (pq.size())
{
// std::cout << "send from A: " << pq.top().node << ' ' << pq.top().currDist << '\n';
writeA(pq.top().node, NODE_BITS);
writeA(pq.top().currDist - lastDist, DIST_BITS);
} else if (otherNode != n + 1)
{
// std::cout << "send from A: " << n << ' ' << -1 << '\n';
writeA(n, NODE_BITS);
writeA(0, DIST_BITS);
}
}
void ReceiveA(bool x)
{
receivedQueue.push(x);
if (receivedQueue.size() == NODE_BITS + DIST_BITS + 1 || (receivedQueue.size() == 1 && receivedQueue.front() == 1))
{
receivePairA();
}
}
int readA(int bits)
{
int res = 0;
for (int i = 0 ; i < bits ; ++i)
{
res <<= 1;
// std::cout << "bitA: " << receivedQueue.front() << ' ' << res << '\n';
res += receivedQueue.front();
receivedQueue.pop();
}
return res;
}
void writeA(int number, int bits)
{
for (int i = bits - 1 ; i >= 0 ; --i)
{
SendA((number & (1 << i)) > 0);
}
}
void InitA(int N, int A, std::vector <int> U, std::vector <int> V,std::vector <int> C)
{
n = N; m = A;
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]});
}
std::fill(dist, dist + n, INF);
pq.push({0, 0});
writeA(0, NODE_BITS);
writeA(0, DIST_BITS);
dist[0] = 0;
}
std::vector <int> Answer()
{
std::vector <int> answer(n);
for (int i = 0 ; i < n ; ++i)
{
answer[i] = dist[i];
}
return answer;
}
#include "Baijan.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
typedef long long llong;
const int DIST_BITS = 9;
const int NODE_BITS = 11;
const int MAXN = 2000 + 10;
const int MAXM = 500000 + 10;
const int INF = 1e9;
static int n, m;
static int lastDist;
static int dist[MAXN];
static bool vis[MAXN];
static std::vector <std::pair <int,int>> g[MAXN];
struct PQelement
{
int node;
int currDist;
friend bool operator < (const PQelement &a, const PQelement &b)
{
return a.currDist > b.currDist || (a.currDist == b.currDist && a.node > b.node);
}
};
static std::priority_queue <PQelement> pq;
static std::queue <int> receivedQueue;
int readB(int);
void writeB(int, int);
void receivePairB();
void ReceiveB(bool x)
{
receivedQueue.push(x);
if (receivedQueue.size() == NODE_BITS + DIST_BITS)
{
receivePairB();
}
}
void receivePairB()
{
int otherNode = readB(NODE_BITS);
int otherDist = lastDist + readB(DIST_BITS);
if (otherNode < n) pq.push({otherNode, otherDist});
// std::cout << "from A: " << otherNode << ' ' << otherDist << '\n';
while (pq.size() && vis[pq.top().node])
{
pq.pop();
}
bool whatShouldSend = false;
PQelement other = {otherNode, otherDist};
if (pq.size() && (otherNode >= n || other < pq.top()))
{
// std::cout << "send from B: " << pq.top().node << ' ' << pq.top().currDist << ' ' << lastDist << '\n';
SendB(0);
writeB(pq.top().node, NODE_BITS);
writeB(pq.top().currDist - lastDist, DIST_BITS);
} else if (pq.size())
{
SendB(1);
}
if (pq.size())
{
auto [node, currDist] = pq.top();
// std::cout << "topB: " << node << ' ' << currDist << "\n";
pq.pop();
vis[node] = true;
dist[node] = currDist;
lastDist = currDist;
for (const auto &[u, edge] : g[node])
{
// std::cout << "B edge: " << u << ' ' << node << ' ' << edge << ' ' << dist[u] << ' ' << dist[node] + edge << '\n';
if (dist[u] > dist[node] + edge)
{
dist[u] = dist[node] + edge;
// assert(!vispu)
pq.push({u, dist[u]});
}
}
}
while (pq.size() && vis[pq.top().node])
{
pq.pop();
}
}
int readB(int bits)
{
int res = 0;
for (int i = 0 ; i < bits ; ++i)
{
res <<= 1;
res += receivedQueue.front();
receivedQueue.pop();
}
return res;
}
void writeB(int number, int bits)
{
for (int i = bits - 1 ; i >= 0 ; --i)
{
// std::cout << "bitB: " << number << ' ' << (number & (1 << i)) << ' ' << ((number & (1 << i)) > 0) << '\n';
SendB((number & (1 << i)) > 0);
}
}
void InitB(int N, int B, std::vector <int> U, std::vector <int> V, std::vector <int> C)
{
n = N; m = B;
for (int i = 0 ; i < B ; ++i)
{
g[U[i]].push_back({V[i], C[i]});
g[V[i]].push_back({U[i], C[i]});
}
std::fill(dist, dist + n, INF);
pq.push({0, 0});
dist[0] = 0;
}
Compilation message
Baijan.cpp: In function 'void receivePairB()':
Baijan.cpp:60:10: warning: unused variable 'whatShouldSend' [-Wunused-variable]
60 | bool whatShouldSend = false;
| ^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
144 ms |
456 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
664 KB |
Output is correct |
2 |
Runtime error |
123 ms |
548 KB |
Execution killed with signal 13 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
113 ms |
532 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
664 KB |
Output is correct |
2 |
Correct |
208 ms |
816 KB |
Output is correct |
3 |
Correct |
157 ms |
13704 KB |
Output is correct |
4 |
Correct |
184 ms |
664 KB |
Output is correct |
5 |
Correct |
164 ms |
10092 KB |
Output is correct |
6 |
Correct |
167 ms |
672 KB |
Output is correct |
7 |
Correct |
150 ms |
664 KB |
Output is correct |
8 |
Correct |
145 ms |
664 KB |
Output is correct |
9 |
Correct |
178 ms |
18468 KB |
Output is correct |
10 |
Correct |
345 ms |
18868 KB |
Output is correct |
11 |
Correct |
357 ms |
35748 KB |
Output is correct |
12 |
Correct |
285 ms |
31020 KB |
Output is correct |
13 |
Correct |
153 ms |
664 KB |
Output is correct |
14 |
Correct |
1 ms |
872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
664 KB |
Output is correct |
2 |
Correct |
208 ms |
816 KB |
Output is correct |
3 |
Correct |
157 ms |
13704 KB |
Output is correct |
4 |
Correct |
184 ms |
664 KB |
Output is correct |
5 |
Correct |
164 ms |
10092 KB |
Output is correct |
6 |
Correct |
167 ms |
672 KB |
Output is correct |
7 |
Correct |
150 ms |
664 KB |
Output is correct |
8 |
Correct |
145 ms |
664 KB |
Output is correct |
9 |
Correct |
178 ms |
18468 KB |
Output is correct |
10 |
Correct |
345 ms |
18868 KB |
Output is correct |
11 |
Correct |
357 ms |
35748 KB |
Output is correct |
12 |
Correct |
285 ms |
31020 KB |
Output is correct |
13 |
Correct |
153 ms |
664 KB |
Output is correct |
14 |
Correct |
1 ms |
872 KB |
Output is correct |
15 |
Correct |
170 ms |
828 KB |
Output is correct |
16 |
Correct |
118 ms |
1008 KB |
Output is correct |
17 |
Correct |
173 ms |
664 KB |
Output is correct |
18 |
Correct |
193 ms |
10268 KB |
Output is correct |
19 |
Correct |
162 ms |
664 KB |
Output is correct |
20 |
Correct |
185 ms |
10544 KB |
Output is correct |
21 |
Correct |
147 ms |
916 KB |
Output is correct |
22 |
Correct |
170 ms |
876 KB |
Output is correct |
23 |
Correct |
256 ms |
22284 KB |
Output is correct |
24 |
Correct |
338 ms |
22664 KB |
Output is correct |
25 |
Correct |
420 ms |
43804 KB |
Output is correct |
26 |
Correct |
334 ms |
37188 KB |
Output is correct |
27 |
Correct |
204 ms |
916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
664 KB |
Output is correct |
2 |
Correct |
208 ms |
816 KB |
Output is correct |
3 |
Correct |
157 ms |
13704 KB |
Output is correct |
4 |
Correct |
184 ms |
664 KB |
Output is correct |
5 |
Correct |
164 ms |
10092 KB |
Output is correct |
6 |
Correct |
167 ms |
672 KB |
Output is correct |
7 |
Correct |
150 ms |
664 KB |
Output is correct |
8 |
Correct |
145 ms |
664 KB |
Output is correct |
9 |
Correct |
178 ms |
18468 KB |
Output is correct |
10 |
Correct |
345 ms |
18868 KB |
Output is correct |
11 |
Correct |
357 ms |
35748 KB |
Output is correct |
12 |
Correct |
285 ms |
31020 KB |
Output is correct |
13 |
Correct |
153 ms |
664 KB |
Output is correct |
14 |
Correct |
1 ms |
872 KB |
Output is correct |
15 |
Correct |
170 ms |
828 KB |
Output is correct |
16 |
Correct |
118 ms |
1008 KB |
Output is correct |
17 |
Correct |
173 ms |
664 KB |
Output is correct |
18 |
Correct |
193 ms |
10268 KB |
Output is correct |
19 |
Correct |
162 ms |
664 KB |
Output is correct |
20 |
Correct |
185 ms |
10544 KB |
Output is correct |
21 |
Correct |
147 ms |
916 KB |
Output is correct |
22 |
Correct |
170 ms |
876 KB |
Output is correct |
23 |
Correct |
256 ms |
22284 KB |
Output is correct |
24 |
Correct |
338 ms |
22664 KB |
Output is correct |
25 |
Correct |
420 ms |
43804 KB |
Output is correct |
26 |
Correct |
334 ms |
37188 KB |
Output is correct |
27 |
Correct |
204 ms |
916 KB |
Output is correct |
28 |
Correct |
239 ms |
664 KB |
Output is correct |
29 |
Correct |
315 ms |
792 KB |
Output is correct |
30 |
Correct |
246 ms |
24224 KB |
Output is correct |
31 |
Correct |
300 ms |
788 KB |
Output is correct |
32 |
Correct |
276 ms |
21392 KB |
Output is correct |
33 |
Correct |
172 ms |
864 KB |
Output is correct |
34 |
Correct |
273 ms |
920 KB |
Output is correct |
35 |
Correct |
182 ms |
896 KB |
Output is correct |
36 |
Correct |
266 ms |
25100 KB |
Output is correct |
37 |
Correct |
451 ms |
24976 KB |
Output is correct |
38 |
Correct |
486 ms |
49044 KB |
Output is correct |
39 |
Correct |
427 ms |
44128 KB |
Output is correct |
40 |
Correct |
195 ms |
1428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
144 ms |
456 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |