#include #include #include #define rep(i, l, n) for (int i = (l); i < (n); i++) #define squ(n) (n) * (n) using namespace std; using namespace atcoder; using ll = long long; using u64 = unsigned long long; template using V = vector; u64 isqrt(u64 n) { u64 ok = 0ll; u64 ng = 4000000000ll; while (ng - ok > 1ll) { u64 mid = (ok + ng) >> 1ll; if (mid * mid <= n) { ok = mid; } else { ng = mid; } } return ok; } int main(void) { int n; cin >> n; V > p(n, V(3)); rep(i, 0, n) { ll x, y, t; cin >> x >> y >> t; p[i] = { x,y,t }; } V > dist(n, V(n, 1000000000000000000ll)); rep(i, 0, n) { dist[i][i] = 0ll; } rep(i, 0, n - 1) { u64 r1 = (u64)(squ(p[i][0]) + squ(p[i][1])); rep(j, i + 1, n) { u64 r2 = (u64)(squ(p[j][0]) + squ(p[j][1])); u64 d = (u64)(squ(p[i][0] - p[j][0]) + squ(p[i][1] - p[j][1])); if (p[i][2] != p[j][2]) { d = r1 + r2 - isqrt(4ll * r1 * r2); } dist[i][j] = dist[j][i] = d; } } rep(k, 0, n) { rep(i, 0, n) { rep(j, 0, n) { dist[i][j] = min(dist[i][j], max(dist[i][k], dist[k][j])); } } } u64 ans = dist[0][n - 1]; cout << ans << endl; return 0; }