Skip to content

Instantly share code, notes, and snippets.

@wiwiho

wiwiho/B.cpp Secret

Created December 19, 2021 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiwiho/c9fe30e1649174721f03158ac7287f1f to your computer and use it in GitHub Desktop.
Save wiwiho/c9fe30e1649174721f03158ac7287f1f to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef long double ld;
using pll = pair<ll, ll>;
pll operator+(pll a, pll b){
return mp(a.F + b.F, a.S + b.S);
}
pll operator-(pll a, pll b){
return mp(a.F - b.F, a.S - b.S);
}
ll cross(pll a, pll b){
return a.F * b.S - a.S * b.F;
}
ld dis(pll a, pll b){
ll x = a.F - b.F, y = a.S - b.S;
return sqrt(x * x + y * y);
}
ld absv(pll v){
return sqrt(v.F * v.F + v.S * v.S);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
ll x, y, n;
cin >> x >> y >> n;
pll a = mp(x, y);
pll c;
cin >> c.F >> c.S;
ld ans = dis(a, c);
for(int i = 1; i <= n; i++){
pll d;
cin >> d.F >> d.S;
pll cd = d - c;
pll v = mp(cd.S, -cd.F);
pll ca = a - c;
ld len = (ld)cross(ca, cd) / cross(cd, v);
ld px = a.F + len * v.F;
ld py = a.S + len * v.S;
ll mnx = min(c.F, d.F), mxx = max(c.F, d.F);
ll mny = min(c.S, d.S), mxy = max(c.S, d.S);
if(mnx <= px && px <= mxx && mny <= py && py <= mxy)
ans = min(ans, abs(len) * absv(v));
ans = min(ans, dis(a, d));
c = d;
}
cout << fixed << setprecision(20) << ans << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment