Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include<list>
  2. #include <numeric>
  3. #include<bitset>
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<algorithm>
  7. #include<vector>
  8. #include<set>
  9. #include<map>
  10. #include<functional>
  11. #include<string>
  12. #include<cstring>
  13. #include<cstdlib>
  14. #include<queue>
  15. #include<utility>
  16. #include<fstream>
  17. #include<sstream>
  18. #include<cmath>
  19. #include<stack>
  20. #include<assert.h>
  21. #include<unordered_map>
  22. #include<unordered_set>
  23.  
  24. #define FastRead        ios_base::sync_with_stdio(0);cin.tie(NULL);
  25. #define fRead           freopen("input.txt","r",stdin);
  26. #define fWrite          freopen("output.txt","w",stdout);
  27.  
  28. #define LL              long long
  29. #define MEM(a,value)    memset(a,value,sizeof(a))
  30. #define NL              "\n"
  31. #define SP              " "
  32. #define debug           cout<<"---VALHALLA---"<<"\n";
  33. #define INF             100000000
  34. #define PI              acos(-1.0)
  35. #define MAX             100005
  36. #define ABS(x)          ((x)<0?-(x):(x))
  37.  
  38. using namespace std;
  39. int n;
  40. int cost[11][2];
  41. int dp[(1<<10)+2][10+2];
  42. int Set(int N,int pos){
  43.     return N | (1<<pos);
  44. }
  45. bool check(int N, int pos){
  46.     return (bool)(N & (1<<pos));
  47. }
  48.  
  49. int call(int mask,int current){
  50.     if(mask==(1<<n)-1){
  51.         return (abs(cost[current][0]-cost[0][0])+abs(cost[current][1]-cost[0][1]));
  52.     }
  53.     if(dp[mask][current]!=-1)return dp[mask][current];
  54.     int ans = 1<<28;
  55.     for(int i=1;i<=n;i++){
  56.  
  57.         if(check(mask,i)==0) {
  58.             int ret = (abs(cost[current][0] - cost[i][0]) + abs(cost[current][1] - cost[i][1])) + call(Set(mask, i), i);
  59.             ans = min(ret, ans);
  60.         }
  61.     }
  62.     return dp[mask][current] = ans;
  63.  
  64.  
  65. }
  66. int main() {
  67.     FastRead
  68.     int t;
  69.     cin>>t;
  70.     while(t--){
  71.         int x,y;
  72.         cin>>x>>y;
  73.         cin>>cost[0][0]>>cost[0][1];
  74.         cin>>n;
  75.         for(int i=1;i<=n;i++){
  76.             cin>>cost[i][0]>>cost[i][1];
  77.         }
  78.         MEM(dp,-1);
  79.         int cost = call(0,0);
  80.         cout<<cost<<endl;
  81.  
  82.     }
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement