//yukicoder@cpp17 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using P = pair; const ll MOD = 998244353; const ll MODx = 1000000007; const int INF = (1<<30)-1; const ll LINF = (1LL<<62LL)-1; const double EPS = (1e-10); P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}}; P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; template vector make_vector(size_t a, T b) { return vector(a, b); } template auto make_vector(size_t a, Ts... ts) { return vector(a, make_vector(ts...)); } /* 確認ポイント cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる 計算量は変わらないが楽できるシリーズ min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる */ /* function corner below */ /* Function corner above */ /* comment outed because can cause bugs __attribute__((constructor)) void initial() { cin.tie(0); ios::sync_with_stdio(false); } */ template class FloydWarshall { C MAX; void run(){ for(int k = 0; n > k; k++){ for(int i = 0; n > i; i++){ for(int j = 0; n > j; j++){ if(dist[i][k] == MAX || dist[k][j] == MAX)continue; dist[i][j] = min(dist[i][j],max(dist[i][k],dist[k][j])); } } } } public: int n; vector> dist;//MAX以上なら辺はなし FloydWarshall(int n_,C MAX = 2e18+1):n(n_),MAX(MAX),dist(n_,vector(n_,MAX)){ for(int i = 0; n > i; i++){ dist[i][i] = 0; } } //双方向 void push(int s,int v,C c){ dist[s][v] = min(dist[s][v],c); dist[v][s] = min(dist[s][v],c); } void update(int s,int v,C c){ dist[s][v] = c; dist[v][s] = c; } void push_p(int s,int v,C c){ dist[s][v] = min(dist[s][v],c); } void update_p(int s,int v,C c){ dist[s][v] = c; } bool negative(){ for(int i = 0; n > i; i++){ if(dist[i][i] < 0)return true; } return false; } bool isInf(int s,int v){ return dist[s][v] == MAX; } void build(){ run(); } }; struct d{ long long x,y,d; }; bool operator<(d a, d b){ return a.x*a.x+a.y*a.y < b.x*b.x+b.y*b.y; } long long dist(d a,d b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } long long dist2(d a, d b){ long long ans = (a.x*a.x+a.y*a.y)+(b.x*b.x+b.y*b.y); return ceil((long double)(a.x*a.x+a.y*a.y)+(b.x*b.x+b.y*b.y)-((long double)2*sqrt(a.x*a.x+a.y*a.y)*sqrt(b.x*b.x+b.y*b.y))); } int main(){ int n;cin>>n; vector A(n); for(int i = 0; n > i; i++){ cin>>A[i].x>>A[i].y>>A[i].d; } FloydWarshall B(n); for(int i = 0; n > i; i++){ for(int j = 1+i; n > j; j++){ if(A[i].d == A[j].d){ B.push(i,j,dist(A[i],A[j])); }else{ B.push(i,j,dist2(A[i],A[j])); } } } B.build(); cout << B.dist[0][n-1] << endl; }