-
[1774] 우주신과의 교감BOJ 2022. 1. 15. 23:33
https://www.acmicpc.net/problem/1774
1774번: 우주신과의 교감
(1,1) (3,1) (2,3) (4,3) 이렇게 우주신들과 황선자씨의 좌표가 주어졌고 1번하고 4번이 연결되어 있다. 그렇다면 1번하고 2번을 잇는 통로를 만들고 3번하고 4번을 잇는 통로를 만들면 신들과 선자씨끼
www.acmicpc.net
<문제>
이미 연결된 간선 가중치테이블의 값을 0으로 설정해둔다.
모든 (i, j)에 대해서, 두 점 사이의 거리를 가중치를 갖는 간선을 생성해주면 남는것은 mst를 구하는 일 밖에 없다.
<소스코드>
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#include <bits/stdc++.h>using namespace std;using ll = long long;using pi = pair<ll, ll>;using p = pair<double, ll>;vector<pi> v;priority_queue<p, vector<p>, greater<p>> q;ll n, m;bool vst[1003];double cost[1003][1003], ans;double c(ll A, ll B) {return sqrt(pow(v[A].first - v[B].first, 2) +pow(v[A].second - v[B].second, 2));}void prim(void) {vst[1] = true;ll i;for (i = 2; i <= n; i++) q.push({cost[1][i], i});while (!q.empty()) {double curCost = q.top().first;ll curIdx = q.top().second;q.pop();if (vst[curIdx] == true) continue;vst[curIdx] = true;ans += curCost;for (i = 1; i <= n; i++)if (vst[i] == false) q.push({cost[curIdx][i], i});}}int main(void) {ios_base::sync_with_stdio(0);cin.tie(0);cin >> n >> m;ll i, j;v.resize(n);for (i = 0; i < n; i++) cin >> v[i].first >> v[i].second;for (i = 0; i < n; i++)for (j = 0; j < n; j++) {if (i == j) continue;cost[i + 1][j + 1] = c(i, j);}for (i = 0; i < m; i++) {ll A, B;cin >> A >> B;cost[A][B] = 0;cost[B][A] = 0;}prim();cout << fixed, cout.precision(2);cout << ans;return 0;}cs 'BOJ' 카테고리의 다른 글
[16208] 귀찮음 (0) 2022.01.17 [11444] 피보나치 수 6 (0) 2022.01.16 [1826] 연료 채우기 (0) 2022.01.15 [13015] 별 찍기 - 23 (0) 2022.01.14 [2485] 가로수 (0) 2022.01.12