#ifndef CLASS_DISJOINT_SET #define CLASS_DISJOINT_SET #include #include #include #include class disjoint_set { private: typedef std::int32_t value_type; std::vector val; public: explicit disjoint_set() : val() {}; explicit disjoint_set(std::size_t n) : val(n, -1) {}; std::size_t size() const { return val.size(); } std::size_t size(std::size_t elem) { return std::size_t(-val[root(elem)]); } std::size_t root(std::size_t elem) { // path halving while (val[elem] >= 0 && val[val[elem]] >= 0) { val[elem] = val[val[elem]]; elem = val[elem]; } return std::size_t(val[elem] >= 0 ? val[elem] : elem); } void link(std::size_t elemx, std::size_t elemy) { elemx = root(elemx); elemy = root(elemy); if (elemx == elemy) return; if (val[elemx] > val[elemy]) { std::swap(elemx, elemy); } val[elemx] += val[elemy]; val[elemy] = elemx; } bool connected(std::size_t elemx, std::size_t elemy) { return root(elemx) == root(elemy); } }; #endif // CLASS_DISJOINT_SET #include #include #include #include using namespace std; int main() { // step #1. input int N; cin >> N; vector X(N), Y(N), T(N); for (int i = 0; i < N; i++) { cin >> X[i] >> Y[i] >> T[i]; } // step #2. calculate distance vector radius(N); for (int i = 0; i < N; i++) { radius[i] = sqrt(X[i] * X[i] + Y[i] * Y[i]); } auto sqr = [](long long x) { return x * x; }; vector > d(N, vector(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { d[i][j] = (T[i] == T[j] ? sqr(X[i] - X[j]) + sqr(Y[i] - Y[j]) : ceil((radius[i] - radius[j]) * (radius[i] - radius[j]))); } } // step #3. main calculation vector > edges; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { edges.push_back(make_pair(i, j)); } } sort(edges.begin(), edges.end(), [&](const pair& p1, const pair& p2) { return d[p1.first][p1.second] < d[p2.first][p2.second]; }); disjoint_set uf(N); long long answer = -1; for (pair i : edges) { uf.link(i.first, i.second); if (uf.connected(0, N - 1)) { answer = d[i.first][i.second]; break; } } // step #4. output cout << answer << endl; return 0; }