#include #include #include #pragma warning(disable: 4996) using namespace std; using namespace atcoder; #define print(n) cout << (n) << endl #define fprint(n) cout << setprecision(16) << (n) << endl #define ceil_div(a, b) (((a) - 1) / (b) + 1) #define rep(i, l, n) for (int i = (l); i < (n); i++) #define itrep(itr, st) for (auto itr = st.begin(); itr != st.end(); itr++) #define ite(i, a) for (auto& i : a) #define last(v) v[v.size() - 1] #define all(x) x.begin(), x.end() #define lb(A, x) (lower_bound(all(A), x) - A.begin()) #define ub(A, x) (upper_bound(all(A), x) - A.begin()) #define INF 4611686018427387903ll #define inf 2147483647 #define mod 1000000007ll #define MOD 998244353ll using str = string; using ll = long long; using u32 = unsigned int; using u64 = unsigned long long; using Pair = pair; using mint = modint1000000007; using Mint = modint998244353; template using V = vector; template using VV = V >; template using PQ = priority_queue, greater >; template using PQR = priority_queue; template using BIT = fenwick_tree; template inline V getList(int n) { V res(n); rep(i, 0, n) { cin >> res[i]; }return res; } template inline VV getGrid(int m, int n) { VV res(m, V(n)); rep(i, 0, m) { res[i] = getList(n); }return res; } template inline void prints(V& vec) { if (vec.size() == 0)return; cout << vec[0]; rep(i, 1, vec.size()) { cout << ' ' << vec[i]; } cout << '\n'; } template inline vector > enumerate(const vector& values) { auto length = values.size(); auto values_with_indices = vector>(length); for (size_t i = 0; i < length; ++i) { values_with_indices[i] = make_tuple(i, values[i]); } return values_with_indices; } inline V dtois(string& s) { V vec = {}; ite(e, s) { vec.push_back(e - '0'); } return vec; } inline V atois(string& s) { V vec = {}; ite(e, s) { vec.push_back(e - 'a'); } return vec; } inline V Atois(string& s) { V vec = {}; ite(e, s) { vec.push_back(e - 'A'); } return vec; } #define squ(x) ((x) * (x)) ll isqrt(ll x) { ll ok = 0; ll ng = inf; while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (squ(mid) <= x) { ok = mid; } else { ng = mid; } } return ok; } int main(void) { int n; cin >> n; V > p = getGrid(n, 3); V > dist(n, V(n, INF)); rep(i, 0, n - 1) { rep(j, i + 1, n) { if (p[i][2] == p[j][2]) { dist[i][j] = dist[j][i] = squ(p[i][0] - p[j][0]) + squ(p[i][1] - p[j][1]); } else { ll r1 = squ(p[i][0]) + squ(p[i][1]); ll r2 = squ(p[j][0]) + squ(p[j][1]); dist[i][j] = dist[j][i] = r1 + r2 - isqrt(4 * r1 * r2); // cout << i << ' ' << j << ' ' << dist[i][j] << endl; } } } V visited(n, INF); visited[0] = 0; PQ > que = {}; que.push({ 0,0 }); while (que.empty() == false) { pair pa = que.top(); que.pop(); ll c_now = pa.first, v_now = pa.second; if (c_now == n - 1) { break; } if (c_now > visited[v_now]) { continue; } rep(v_next, 0, n) { ll c_next = max(c_now, dist[v_now][v_next]); // cout << v_now << ' ' << v_next << ' ' << dist[v_now][v_next] << endl; if (c_next >= visited[v_next]) { continue; } visited[v_next] = c_next; que.push({ c_next,v_next }); } } print(visited[n - 1]); return 0; }