Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<list>
- #include <numeric>
- #include<bitset>
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<vector>
- #include<set>
- #include<map>
- #include<functional>
- #include<string>
- #include<cstring>
- #include<cstdlib>
- #include<queue>
- #include<utility>
- #include<fstream>
- #include<sstream>
- #include<cmath>
- #include<stack>
- #include<assert.h>
- #include<unordered_map>
- #include<unordered_set>
- #define FastRead ios_base::sync_with_stdio(0);cin.tie(NULL);
- #define fRead freopen("input.txt","r",stdin);
- #define fWrite freopen("output.txt","w",stdout);
- #define LL long long
- #define MEM(a,value) memset(a,value,sizeof(a))
- #define NL "\n"
- #define SP " "
- #define debug cout<<"---VALHALLA---"<<"\n";
- #define INF 100000000
- #define PI acos(-1.0)
- #define MAX 100005
- #define ABS(x) ((x)<0?-(x):(x))
- using namespace std;
- int n;
- int cost[11][2];
- int dp[(1<<10)+2][10+2];
- int Set(int N,int pos){
- return N | (1<<pos);
- }
- bool check(int N, int pos){
- return (bool)(N & (1<<pos));
- }
- int call(int mask,int current){
- if(mask==(1<<n)-1){
- return (abs(cost[current][0]-cost[0][0])+abs(cost[current][1]-cost[0][1]));
- }
- if(dp[mask][current]!=-1)return dp[mask][current];
- int ans = 1<<28;
- for(int i=1;i<=n;i++){
- if(check(mask,i)==0) {
- int ret = (abs(cost[current][0] - cost[i][0]) + abs(cost[current][1] - cost[i][1])) + call(Set(mask, i), i);
- ans = min(ret, ans);
- }
- }
- return dp[mask][current] = ans;
- }
- int main() {
- FastRead
- int t;
- cin>>t;
- while(t--){
- int x,y;
- cin>>x>>y;
- cin>>cost[0][0]>>cost[0][1];
- cin>>n;
- for(int i=1;i<=n;i++){
- cin>>cost[i][0]>>cost[i][1];
- }
- MEM(dp,-1);
- int cost = call(0,0);
- cout<<cost<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement