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<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ll long long
pii delta[4] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
pii add(pii lh, pii rh){
return {lh.first+rh.first, lh.second+rh.second};
}
vector<int> block_list;
vector<vector<int>> adj;
map<pii, int> blocks;
bool check_presence(pii pos){
if(blocks.find(pos)!= blocks.end()){
return true;
}
return false;
}
vector<pii> get_adj(pii pos){
vector<pii> res;
for(int i = 0; i<4; i++){
pii pos2 = add(pos, delta[i]);
if(check_presence(pos2)){
res.push_back(pos2);
}
}
return res;
}
void BFS(int origin){
for(int i = 0; i<block_list.size(); i++){
block_list[i] =-1;
}
block_list[origin] = 0;
queue<pair<int, int>> q;
q.push({origin, 0});
while(q.size()>0){
auto cur = q.front();
auto u = cur.first;
auto d= cur.second;
q.pop();
for(auto v: adj[u]){
if(block_list[v] == -1){
block_list[v]= d+1;
q.push({v, d+1});
}
}
}
}
const long long mod = 1000000000;
int DistanceSum(int N, int *X, int *Y) {
for(int i = 0; i<N; i++){
blocks[{X[i], Y[i]}] = i;
}
block_list.resize(N, -1);
adj.resize(N);
for(int i = 0; i<N; i++){
for(auto v: get_adj({X[i], Y[i]})){
adj[i].push_back(blocks[v]);
}
}
long long res = 0;
for(int i = 0; i<N; i++){
BFS(blocks[{X[i], Y[i]}]);
for(int i = 0; i<N; i++){
res = res + block_list[i];
}
}
res/=2;
return res%mod;
}
Compilation message (stderr)
city.cpp: In function 'void BFS(int)':
city.cpp:34:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0; i<block_list.size(); i++){
| ~^~~~~~~~~~~~~~~~~~
# | 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... |