SuitNdtie

SpaceShip PROG1174

Mar 29th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. typedef struct{
  6.     int x,y,z;
  7. }pos;
  8. typedef struct{
  9.     int a,b,c;
  10. }part;
  11. int dist(pos a,pos b){
  12.     return (((a.x - b.x)*(a.x - b.x)) + ((a.y - b.y)*(a.y - b.y)) + ((a.z - b.z)*(a.z - b.z)));
  13. }
  14.  
  15. int const M = 11;
  16. int m;
  17. int n;
  18. bool visited[M];
  19. pos ShopPos[M];
  20. part ShopPart[M];
  21.  
  22. int mincost = 2e9;
  23. part buying(part bought,part willbuy){
  24.     bought.a += willbuy.a;
  25.     bought.b += willbuy.b;
  26.     bought.c += willbuy.c;
  27.     return bought;
  28. }
  29. bool check(part bought){
  30.     return (bought.a >= n) && (bought.b >= n) && (bought.c >= n);
  31. }
  32.  
  33. int path[M];
  34. void recur(int Pos){
  35. //  visited[now] = true;
  36.     int cost = 0;
  37.     part bought = {0,0,0};
  38.     for(int i=1;i<Pos;i++){
  39. //      printf("%d ",path[i]);
  40.         cost += dist(ShopPos[path[i-1]],ShopPos[path[i]]);
  41.         bought = buying(bought,ShopPart[path[i-1]]);
  42.     }
  43.     bought = buying(bought,ShopPart[path[Pos-1]]);
  44. //  printf("Test bought : %d %d %d ,",bought.a,bought.b,bought.c);
  45.     if(check(bought)){
  46. //      printf("Enough : ");
  47.         if(cost < mincost){
  48.             mincost = cost;
  49.         }
  50.     }
  51. //  printf("cost : %d <- %s ",cost,(check(bought) ? "Enough" : "Not enough"));
  52. //  printf("\n");
  53.     if(Pos > m)return;
  54.     for(int i=1;i<=m;i++){
  55.         if(visited[i])continue;
  56.        
  57.         visited[i] = true;
  58.         path[Pos] = i;
  59.         recur(Pos+1);
  60.         visited[i] = false;
  61.     }
  62. }
  63.  
  64. int main(){
  65.    
  66.     scanf("%d",&n);
  67.     part NeedPart;NeedPart.a = n;NeedPart.b = n;NeedPart.c = n;
  68.    
  69.     pos start;
  70.     scanf("%d %d %d",&start.x,&start.y,&start.z);
  71.    
  72.     scanf("%d",&m);
  73.     path[0] = 0;
  74.     ShopPos[0] = start;
  75.     ShopPart[0] = {0,0,0};
  76.     for(int i=1;i<=m;i++){
  77.         scanf("%d %d %d",&ShopPos[i].x,&ShopPos[i].y,&ShopPos[i].z);
  78.         scanf("%d %d %d",&ShopPart[i].a,&ShopPart[i].b,&ShopPart[i].c);
  79.     }
  80.     recur(1);
  81.     printf("%d",mincost);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment