Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define rep(i, a, b) for(int i = a; i < (b); ++i)
- #define all(x) begin(x), end(x)
- #define sz(x) (int)(x).size()
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- template <typename T> pair<T, vector<int>>
- weighted_matching(vector<vector<T>> &C) {
- int i = sz(C), m = sz(C[0]), c, j, s, r;
- vector<T> dist(m), potential(m);
- vi row_match(i), col_match(m, -1), cols(m), prev(m);
- T d, nd, cost = 0;
- for (; i--;) {
- rep(c, 0, m) dist[c] = C[i][c], cols[c] = c, prev[c] = i;
- for (s = 0;;) {
- for (j = s; j < m; j++) if (c = cols[j],
- nd = dist[c] - potential[c], j == s || d > nd)
- d = nd, swap(cols[s], cols[j]);
- if (!~(r = col_match[c = cols[s++]])) break;
- rep(j, 0, m) if (dist[j] > (nd = C[r][j] - C[r][c] +
- dist[c])) dist[j] = nd, prev[j] = r;
- }
- for (cost += dist[c]; s--;) j = cols[s],
- potential[j] = dist[j] - d;
- for (; r != i; swap(c, row_match[r]))
- r = col_match[c] = prev[c];
- }
- return {cost, row_match};
- }
- const double inf = 1e10;
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- ll n, s, t, q;
- cin >> n >> s >> t >> q;
- q *= q;
- vector<vector<double>> cost(t, vector<double>(s, inf));
- vector<pii> pos;
- vi height;
- rep(i, 0, n) {
- int x, y, h;
- cin >> x >> y >> h;
- pos.emplace_back(x, y);
- height.push_back(h);
- }
- vi istown(n);
- vi is_spring(n);
- vi spring_ind(n);
- int ind = 0;
- rep(i, 0, s) {
- int spr_ind;
- cin >> spr_ind;
- spr_ind--;
- is_spring[spr_ind] = 1;
- spring_ind[spr_ind] = ind++;
- }
- rep(i, 0, t) {
- int town_ind;
- cin >> town_ind;
- town_ind--;
- istown[town_ind] = 1;
- }
- ind = 0;
- rep(i, 0, n) {
- if (istown[i]) {
- priority_queue<pair<double, int>> pq;
- vi vis(n);
- pq.emplace(0, i);
- while (pq.size()) {
- double d;
- int u;
- tie(d, u) = pq.top();
- pq.pop();
- if (vis[u]) continue;
- vis[u] = 1;
- if (is_spring[u]) cost[ind][spring_ind[u]] = -d;
- rep(j, 0, n) {
- if (height[u] >= height[j]) continue;
- double dist = pow(pos[u].first - pos[j].first, 2) + pow(pos[u].second - pos[j].second, 2) + pow(height[u] - height[j], 2);
- if (dist > q) continue;
- pq.emplace(d - sqrt(dist), j);
- }
- }
- ind++;
- }
- }
- auto [c, _] = weighted_matching(cost);
- if (c >= inf) cout << "IMPOSSIBLE";
- else cout << fixed << setprecision(15) << c;
- }
Advertisement
Add Comment
Please, Sign In to add comment