-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeepers.cpp
51 lines (45 loc) · 920 Bytes
/
beepers.cpp
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
#include<iostream>
#include<climits>
#include<cstdlib>
#include<cstring>
using namespace std;
#define MAX 11
int n1;
int x[MAX],y[MAX];
int dist[MAX][MAX];
int memo[MAX][1<<MAX];
int solve(int index,int mask) {
if(mask==(1<<(n1+1))-1)
return dist[index][0];
if(memo[index][mask]!=-1)
return memo[index][mask];
memo[index][mask]=INT_MAX;
for(int i=0;i<=n1;++i)
if(i!=index && !(mask & (1<<i)))
memo[index][mask]=min(memo[index][mask],solve(i,mask^(1<<i))+dist[index][i]);
return memo[index][mask];
}
int main()
{
int t;
//int x[MAX],y[MAX];
int m,n;
cin>>t;
while(t--) {
cin>>m>>n;
cin>>x[0]>>y[0];
cin>>n1;
for(int i=1;i<=n1;++i) {
cin>>x[i]>>y[i];
}
//calculate the distance betw the beepers
for(int i=0;i<=n1;++i) {
for(int j=0;j<=n1;++j) {
dist[i][j]=abs(x[i]-x[j])+abs(y[i]-y[j]);
}
}
memset(memo,-1,sizeof((memo)));
cout<<"The shortest path has length "<<solve(0,1)<<endl;
}
return 0;
}