#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
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++){
| ~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
348 KB |
Output is correct |
2 |
Correct |
19 ms |
576 KB |
Output is correct |
3 |
Correct |
50 ms |
604 KB |
Output is correct |
4 |
Correct |
51 ms |
600 KB |
Output is correct |
5 |
Correct |
95 ms |
684 KB |
Output is correct |
6 |
Correct |
73 ms |
600 KB |
Output is correct |
7 |
Correct |
93 ms |
680 KB |
Output is correct |
8 |
Correct |
74 ms |
600 KB |
Output is correct |
9 |
Correct |
70 ms |
600 KB |
Output is correct |
10 |
Correct |
67 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1098 ms |
2896 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1054 ms |
2908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |