-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2DTravelling.cpp
More file actions
43 lines (41 loc) · 1007 Bytes
/
Copy path2DTravelling.cpp
File metadata and controls
43 lines (41 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
int t;
cin>>t;
while(t--){
int n,k,a,b;
cin>>n>>k>>a>>b;
vector<pair<int,int>>p;
for(int i=0; i<n; i++){
int temp1,temp2;
cin>>temp1>>temp2;
p.push_back({temp1,temp2});
}
int x1,y1,x2,y2;
x1=p[a-1].first;
y1=p[a-1].second;
x2=p[b-1].first;
y2=p[b-1].second;
int ans1=abs(x2-x1) + abs(y2-y1);
int d1=LLONG_MAX;
for(int i=0; i<k; i++){
int cost=abs(x1-p[i].first) + abs(y1-p[i].second);
d1=min(d1,cost);
}
int d2=LLONG_MAX;
for(int i=0; i<k; i++){
int cost=abs(x2-p[i].first) + abs(y2-p[i].second);
d2=min(d2,cost);
}
int ans2=d1+d2;
int ans=min(ans1,ans2);
if(k==0){
cout<<ans1<<endl;
}
else{
cout<<ans<<endl;
}
}
}