Untitled

 avatar
unknown
plain_text
a year ago
2.4 kB
13
Indexable
ho den
/*#include<iostream>
using namespace std;

struct Point{
	int x, y;
};

struct Wormhole{
	Point A, B;
	int K;
};

int abs(int a){
	if(a<0) return -a;
	return a;
}


int dist(const Point& p1, const Point& p2){
	return abs(p1.x - p2.x) + abs(p1.y - p2.y);
}

int minDist;
Point start, endPoint;
Wormhole holes[5];
bool used[5];
int t, tc, n;

void backtrack(Point current, int curDist){
	if(curDist >= minDist) return;
	int directDist = dist(current, endPoint);
	minDist = min(minDist, (curDist + directDist));
	for(int i=0; i<n; i++){
		if(!used[i]){
			used[i] = true;
			int distToA = dist(current, holes[i].A);
			backtrack(holes[i].B, curDist + distToA + holes[i].K);
			int distToB = dist(current, holes[i].B);
			backtrack(holes[i].A, curDist + distToB + holes[i].K);
			used[i] = false;
		}

	}
}

int main(){
	freopen("input.txt", "r", stdin);
	cin>>t;
	for(tc =1; tc<=t; tc++){
		cin>>n;
		cin>>start.x>>start.y>>endPoint.x>>endPoint.y;
		for(int i=0; i<n; ++i){
			cin>>holes[i].A.x>>holes[i].A.y>>holes[i].B.x>>holes[i].B.y>>holes[i].K;
		}
		minDist = dist(start, endPoint);
		for(int i=0; i<n; i++){
			used[i] = false;
		}
		backtrack(start, 0);
		cout<<minDist<<endl;

	}
	return 0;
}
*/
#include<iostream>
using namespace std;

int t, tc, n;
int startX, startY, endX, endY;
int holes[20][5];
int minDist;
bool used[5];


int abs(int a){
	if(a<0) return -a;
	return a;
}

int dist(int x1, int y1, int x2, int y2){
	return abs(x1-x2) + abs(y1-y2);
}

void backtrack(int x, int y, int curDist){
	if(curDist>= minDist) return;
	int direcDist =dist(x, y, endX, endY);
	minDist = min(minDist, (curDist + direcDist));
	for(int i=0; i<n; i++){
		if(!used[i]){
			used[i] = true;
			int distToA = dist(x, y, holes[i][0], holes[i][1]);
			backtrack(holes[i][2], holes[i][3], curDist + distToA + holes[i][4]);
			int distToB = dist(x, y, holes[i][2], holes[i][3]);
			backtrack(holes[i][0], holes[i][1], curDist + distToA + holes[i][4]);
			used[i] = false;
		}
	}
}

int main(){
	freopen("input.txt", "r", stdin);
	cin>>t;
	for(tc =1; tc<=t; tc++){
		cin>>n;
		cin>>startX>>startY>>endX>>endY;
		for(int i=0; i<n; ++i){
			cin>>holes[i][0]>>holes[i][1]>>holes[i][2]>>holes[i][3]>>holes[i][4];
		}
		minDist = dist(startX, startY, endX, endY);
		for(int i=0; i<n; i++){
			used[i] = false;
		}
		backtrack(startX, startY, 0);
		cout<<minDist<<endl;
	}
	return 0;
}



Editor is loading...
Leave a Comment