跳到正文

题解

题解:P12298 [ICPC 2022/2023 WF] Carl 的假期

EzcapeTheUniverse约 8 分钟
原文

这是 2026年1月31日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年1月31日
收录
2026年1月31日
更新
2026年11月18日
篇幅
约 8 分钟
题目
P12298

版本

  1. v2当前
  2. v1

题外话

这道题是暑假集训和一位机房大佬组团开黑搞出来的,近期突然看见这道题遂复刷然后切了

下面表述如果有不准确的欢迎批评指出/bx

题意:

给定你两个不交的四棱锥FF、GG,求从FF的顶端走到GG的顶端的最小距离。

为了以下表述方便,我们对于一个四棱锥X,记top(X)top(X)为其顶端的点坐标(有x,y,z三个维度),H(X)H(X)为其高度(即top的z坐标)

FirstStepFirst Step

我们记a0a0为点(x0,y0)(x0,y0), a1a1为点(x1,y1)(x1,y1) …\dots

首先题目给的输入格式是FF和GG的任意一条边以及其高度,太阴了

其实就是把向量a0a0 →\to a1a1逆时针转90°90°得到a2a2,同理得到a3a3

然后取a0a0,a2a2构成的线段求重点就是当前四棱锥的toptop的横纵坐标了

(建议参照代码,具体怎么求不过多赘述了)

SecondStepSecond Step

恭喜你把FF,GG弄出来了,我们思考怎么算出答案

不难发现(看图能看出来):答案是 F顶端到底端的距离F顶端到底端的距离 + F底端到G底端的距离F底端到G底端的距离 + G底端到G顶端的距离G底端到G顶端的距离

先想一下这大概要怎么求,首先对于以上三个部分,如果把他们的距离表示成函数f1,f2,f3f_1,f_2,f_3,不难发现这三个以及他们相邻两个相加的函数是凸函数,所以直接三分!!

ThirdStepThird Step

既然我们知道要三分搞了,接下来怎么办?

我们不妨假设从top(F)top(F) →\to PP →\to QQ →\to top(G)top(G)是我们的最短路径,其中PP,QQ分别在FF,GG的底边上

现在PP,QQ未知,怎么求?

对于这种问题我们不妨先考虑:假设一个点已知,求另外一个。所以我们先假设PP已知,那么这个时候f1f_1就恒定了,令g(P)=f2+f3g(P)=f_2+f_3。

那么g(P)=f2+f3=dist(P,Q)+dist(Q,top(G))g(P)=f_2+f_3=dist(P,Q)+dist(Q,top(G))

其中dist(P,Q)dist(P,Q)=(XP−XQ)2+(YP−YQ)2\sqrt{(X_P-X_Q)^2+(Y_P-Y_Q)^2},而dist(Q,top(G))dist(Q,top(G))呢?这个勾股定理其实就ok了。

dist(Q,top(G))dist(Q,top(G))=(XQ−Xtop(G))2+H(G)2\sqrt{(X_Q-X_{top(G)})^2+H(G)^2}

由SecondStepSecondStep可知,gg是凸函数,所以能用三分求出最小值。

太好辣,但是还没有求完 因为我们“假设”PP是定值,不慌,现在问题是f1f_1不是定值,而答案是f1+g(P)f_1+g(P),由SecondStepSecondStep可知,这个也是个凸函数,还是三分 这里不过多赘述了 和上面同理 主要看代码

FourthStepFourthStep

做完了吗?No!! 上述PP,QQ直接用(x,y)(x,y)的形式去弄太逊了,而且不好弄

别忘了ThirdStepThirdStep里面说的:“其中PP,QQ分别在FF,GG的底边上”

那么也就是说,我们的PP,QQ可以用别的方法表示出来

我们令t∈[0,1]t\in[0,1], 假设点XX在线段ABAB上。

这个时候XX可以表示为A+t∗(B−A)A+t*(B-A) (注意这里的符号是代码里对于Point类型声明的运算符)

最后答案就是枚举一遍FF,GG的四条边每一对都匹配一遍,最后取minmin就好了

恭喜你AC了,剩下的代码见:

C++
#include <bits/stdc++.h>
namespace Head {
  #define ll long long
  #define u32 unsigned
  #define Int __int128
  #define DB double
  #define pb emplace_back
  #define SZ(a) (int)(a).size()
  #define all(a) (a).begin(),(a).end()
  #define FIR first
  #define SEC second
  #define debug(x) std::cout << #x << " = " << x << '\n'
  void IOS(){std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);}
  void File(){freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);}
  const int inf = 2E9, mod = 998244353;
  const ll INF = 2E18; std::mt19937 rnd(std::random_device{}());
  int Tc;
} using namespace Head;
using namespace std;

const DB EPS = 1E-9;

DB distance(DB x, DB y) {
  return (DB)sqrt(x * x + y * y);
}
using Tp = DB;
struct Point {
  Tp x, y;
  Point() {}
  Point(Tp X, Tp Y) {
    x = X, y = Y;
  }
};
Point operator+(const Point &A, const Point &B) {
  return {A.x + B.x, A.y + B.y};
}
Point operator-(const Point &A, const Point &B) {
  return {A.x - B.x, A.y - B.y};
}
Point operator*(const DB &K, const Point &A) {
  return {K * A.x, K * A.y};
}
Tp Dist(const Point &A, const Point &B) {
  Tp X = A.x - B.x, Y = A.y - B.y;
  return sqrt(X * X + Y * Y);
}
using Z = Point;
using Edge = pair<Z, Z>;

struct Pyramid {
  Z a[5], top;
  int H;
  void read() {
    for (int i = 0; i < 2; i++) {
      cin >> a[i].x >> a[i].y;
    }
    cin >> H;
  }
  void Solve() {
    a[2] = {a[1].x - a[1].y + a[0].y, a[1].y + a[1].x - a[0].x};
    a[3] = {a[0].x - a[1].y + a[0].y, a[0].y + a[1].x - a[0].x};
    a[4] = a[0];
    top = 0.5 * (a[2] + a[0]);
  }
} F, G;

DB ans = 1E100;
DB Calc(Z P, Edge E) {
  DB L = 0, R = 1, res;
  while (R - L > EPS) {
    DB m1 = L + (R - L) / 3;
    DB m2 = R - (R - L) / 3;
    Z P1 = E.FIR + m1 * (E.SEC - E.FIR);
    Z P2 = E.FIR + m2 * (E.SEC - E.FIR);
    DB v1 = Dist(P, P1) + distance(Dist(P1, G.top), G.H);
    DB v2 = Dist(P, P2) + distance(Dist(P2, G.top), G.H);
    res = v1;
    if (v1 < v2) R = m2;
    else L = m1;
  }
  return res;
}

DB getans(Edge e1, Edge e2) {
  DB L = 0, R = 1, res;
  while (R - L > EPS) {
    DB m1 = L + (R - L) / 3;
    DB m2 = R - (R - L) / 3;
    Z P1 = e1.FIR + m1 * (e1.SEC - e1.FIR);
    Z P2 = e1.FIR + m2 * (e1.SEC - e1.FIR);
    DB v1 = Calc(P1, e2) + distance(Dist(P1, F.top), F.H);
    DB v2 = Calc(P2, e2) + distance(Dist(P2, F.top), F.H);
    res = v1;
    if (v1 < v2) R = m2;
    else L = m1;
  }
  return res;
}

int main() {
  IOS();

  F.read(), G.read();
  F.Solve(), G.Solve();

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      ans = min(ans, getans({F.a[i], F.a[i + 1]}, {G.a[j], G.a[j + 1]}));
    }
  }

  cout << fixed << setprecision(9) << ans << '\n';

  return 0;
}