-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwormhole-1.cpp
More file actions
65 lines (62 loc) · 1.66 KB
/
Copy pathwormhole-1.cpp
File metadata and controls
65 lines (62 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <math.h>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct wormhole{
int sx, sy, dx, dy, cost_wh;
};
struct point{
int x, y;
};
int ans = INT_MAX;
int dist(point x, point y){
return abs(x.x-y.x)+abs(x.y-y.y);
}
void solve(point src, point dest, vector<wormhole> &wormholedata, vector<int>&vis, int curr_cost){
int temp = curr_cost+dist(src, dest);
ans=min(ans, temp);
for(int i=0; i<wormholedata.size(); i++){
if(vis[i]==0){
vis[i]=1;
point temp1, temp2;
temp1.x = wormholedata[i].sx;
temp1.y = wormholedata[i].sy;
temp2.x = wormholedata[i].dx;
temp2.y = wormholedata[i].dy;
int value1 = curr_cost+dist(src, temp1)+wormholedata[i].cost_wh;
solve(temp2, dest, wormholedata, vis, value1);
int value2 = curr_cost+dist(src, temp2)+wormholedata[i].cost_wh;
solve(temp1, dest, wormholedata, vis, value2);
vis[i]=0;
}
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int Sx, Sy, Dx, Dy;
cin>>Sx>>Sy>>Dx>>Dy;
point s, d;
s.x=Sx;
s.y=Sy;
d.x=Dx;
d.y=Dy;
ans = INT_MAX;
vector<wormhole> wormholedata(n);
vector<int> vis(10000, 0);
for(int i=0; i<n; i++){
cin>>wormholedata[i].sx>>wormholedata[i].sy>>wormholedata[i].dx>>wormholedata[i].dy>>wormholedata[i].cost_wh;
}
solve(s, d, wormholedata, vis, 0);
cout<<ans<<endl;
}
}