Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<cmath>
- using namespace std;
- typedef struct{
- int x,y,z;
- }pos;
- typedef struct{
- int a,b,c;
- }part;
- int dist(pos a,pos b){
- 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)));
- }
- int const M = 11;
- int m;
- int n;
- bool visited[M];
- pos ShopPos[M];
- part ShopPart[M];
- int mincost = 2e9;
- part buying(part bought,part willbuy){
- bought.a += willbuy.a;
- bought.b += willbuy.b;
- bought.c += willbuy.c;
- return bought;
- }
- bool check(part bought){
- return (bought.a >= n) && (bought.b >= n) && (bought.c >= n);
- }
- int path[M];
- void recur(int Pos){
- // visited[now] = true;
- int cost = 0;
- part bought = {0,0,0};
- for(int i=1;i<Pos;i++){
- // printf("%d ",path[i]);
- cost += dist(ShopPos[path[i-1]],ShopPos[path[i]]);
- bought = buying(bought,ShopPart[path[i-1]]);
- }
- bought = buying(bought,ShopPart[path[Pos-1]]);
- // printf("Test bought : %d %d %d ,",bought.a,bought.b,bought.c);
- if(check(bought)){
- // printf("Enough : ");
- if(cost < mincost){
- mincost = cost;
- }
- }
- // printf("cost : %d <- %s ",cost,(check(bought) ? "Enough" : "Not enough"));
- // printf("\n");
- if(Pos > m)return;
- for(int i=1;i<=m;i++){
- if(visited[i])continue;
- visited[i] = true;
- path[Pos] = i;
- recur(Pos+1);
- visited[i] = false;
- }
- }
- int main(){
- scanf("%d",&n);
- part NeedPart;NeedPart.a = n;NeedPart.b = n;NeedPart.c = n;
- pos start;
- scanf("%d %d %d",&start.x,&start.y,&start.z);
- scanf("%d",&m);
- path[0] = 0;
- ShopPos[0] = start;
- ShopPart[0] = {0,0,0};
- for(int i=1;i<=m;i++){
- scanf("%d %d %d",&ShopPos[i].x,&ShopPos[i].y,&ShopPos[i].z);
- scanf("%d %d %d",&ShopPart[i].a,&ShopPart[i].b,&ShopPart[i].c);
- }
- recur(1);
- printf("%d",mincost);
- }
Advertisement
Add Comment
Please, Sign In to add comment