邮局

题意

$n(1\le n\le50)$ 个人, $m(1\le m \le 25)$ 个车站, 选 $k(1\le k\le 10)$ 个车站, 每个人去离他最近的车站, 输出路径和最短的方案.

题解

搜索剪枝, 从后往前搜索, 若对于当前状态, 某一点无法对任意一个人松弛, 就标记这个点, 之后的搜索都不再访问.

代码

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define fro1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define memcp(a,b) memcpy(a,b,sizeof(b))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef double db;
typedef long long ll;
typedef int itn;
int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}
int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}
int in(int &a,int &b){return scanf("%d%d",&a,&b);}
int in(ll &a){return scanf("%lld",&a);}
int in(int &a){return scanf("%d",&a);}
int in(char *s){return scanf("%s",s);}
int in(char &s){return scanf("%c",&s);}
// int in(db &a){return scanf("%Lf",&a);}
void out(int a){printf("%d",a);}
void outln(int a){printf("%d\n",a);}
void out(ll a){printf("%lld",a);}
void outln(ll a){printf("%lld\n",a);}
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const db eps = 1e-8;
const int N = 2.1e5;
const ll mod = 1e9+7;
int sign(db a) { return a < -eps ? -1 : a > eps;}
int db_cmp(db a, db b){ return sign(a-b); }

int n,m,k;
struct point{
int x,y,no;
}people[55],mail[33];
db G[55][33];
point result[11],tmp_res[11];
db ans=1e18,sum,min_dis[55];
bool Not[33];
db get_dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void dfs(int depth,int index){
if(depth==k){
if(sum<ans){
ans=sum;
for0(i,k)result[i]=tmp_res[i];
}
}else{
db tmp[55],tsum=sum;
bool flag2=0;//标记 [index,m-k+depth] 这些点里有没有能够至少松弛一个人的点
for0(i,n)tmp[i]=min_dis[i];
for(int i=m-k+depth;i>=index;i--){ //当前已经取了 depth 个, 为了保证能够取得 k 个,所以我们最多取到 m-k+depth 点, 从这一点开始往前搜索
if(Not[i])continue;
bool flag=0;//标记 i 是否能够至少松弛一个人
if(depth==0){
sum=0;
for0(j,n){
min_dis[j]=G[j][i];
sum+=min_dis[j];
}
flag=1;
}else{
// for0(j,n)min_dis[j]=tmp[j];
sum=tsum;
for0(j,n){
if(G[j][i]<tmp[j]){
sum-=tmp[j]-G[j][i];
min_dis[j]=G[j][i];
flag=1;
}else min_dis[j]=tmp[j];
}
}
if(flag){
tmp_res[depth]=mail[i];
dfs(depth+1,i+1);
flag2=1;
}else{
Not[i]=1;
}
}
if(!flag2){//若 [index,m-k+depth] 这些点里没有能够松弛的, 就取 m-k+depth
tmp_res[depth]=mail[m-k+depth+1];
dfs(depth+1,m-k+depth+1);
}else{
for0(j,n)min_dis[j]=tmp[j];
sum=tsum;
}
}
}

int main() {
in(n,m,k);
for0(i,n){
in(people[i].x,people[i].y);
}
for0(i,m){
in(mail[i].x,mail[i].y);
mail[i].no=i+1;
for0(j,n){
G[j][i]=get_dis(people[j],mail[i]);
}
}
dfs(0,0);
for0(i,k)printf("%d ",result[i].no);
puts("");
return 0;
}