隐藏
「雅礼day6」通往天堂的路star way to heaven - 最小生成树 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「雅礼day6」通往天堂的路star way to heaven - 最小生成树

题目大意


题目分析

这道题和之前的镜面通道有类似之处。
考虑,如果得到一个答案$ans$,如何验证其是否合法。
以$ans$为半径构成的圆覆盖的棋盘上拥有一条从左到右的通路。

若$dist(p_1,p_2)\lt2ans$显然这条边就不能走了。
若我们将所有的点建成完全图(上下边界不存在横坐标),实际上我们要穿过所有连接上下边界的路径,那么我们的答案显然是某一条最大边最小的路径上的最大边。

这不就是最小生成树吗?

所以最后我们求出最小生成树上连接上下边界的路径上的最大边即可。


代码

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
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
inline const int Get_Int() {
int num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
const int maxn=6005;
struct Edge {
int from,to;
LL dist;
};
vector<Edge>edges[maxn];
void AddEdge(int x,int y,LL v) {
edges[x].push_back((Edge) {
x,y,v
});
}
int n,m,k,vst[maxn],From[maxn];
LL dist[maxn],x[maxn],y[maxn],ans=0;
LL Dist(int a,int b) {
if(a>k||b>k)return (y[a]-y[b])*(y[a]-y[b]);
return (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);
}
void Prim() {
for(int i=1; i<=k+2; i++)dist[i]=1e17;
dist[k+1]=0;
for(int i=1; i<=k+2; i++) {
LL Min=1e17;
int id=0;
for(int j=1; j<=k+2; j++)
if(!vst[j]&&dist[j]<Min) {
Min=dist[j];
id=j;
}
vst[id]=1;
if(From[id]) {
LL d=Dist(id,From[id]);
AddEdge(id,From[id],d);
AddEdge(From[id],id,d);
}
for(int j=1; j<=k+2; j++) {
LL d=Dist(id,j);
if(!vst[j]&&d<dist[j]) {
dist[j]=d;
From[j]=id;
}
}
}
}
void Dfs(int Now,int father,LL Max) {
if(Now==k+2) {
ans=Max;
return;
}
for(auto& e:edges[Now]) {
int Next=e.to;
if(Next==father)continue;
Dfs(Next,Now,max(Max,e.dist));
}
}
int main() {
n=Get_Int();
m=Get_Int();
k=Get_Int();
for(int i=1; i<=k; i++) {
x[i]=Get_Int();
y[i]=Get_Int();
}
y[k+2]=m;
Prim();
Dfs(k+1,0,0);
printf("%0.8lf\n",sqrt(ans)/2);
return 0;
}
姥爷们赏瓶冰阔落吧~