-
algospot - lanAlgorithm/알고스팟 2020. 1. 31. 17:00123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101//algospot - lan#include <iostream>#include <vector>#include <cmath>#include <algorithm>using namespace std;int N, M;vector<int> x;vector<int> y;vector<pair<double, pair<int, int>>> edges;struct DisjointSet{vector<int> parent, rank;DisjointSet(int n) : parent(n), rank(n, 1){for(int i = 0; i<n; ++i)parent[i] = i;}int find(int u){if(u == parent[u]) return u;return parent[u] = find(parent[u]);}void merge(int u, int v){u = find(u); v = find(v);if(u == v) return;if(rank[u] > rank[v]) swap(u, v);parent[u] = v;if(rank[u] == rank[v]) ++rank[v];}};double distance(double x1, double x2, double y1, double y2){double ret = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));return ret;}void init(DisjointSet& sets){edges.clear();x = y = vector<int>(N);for(int i = 0; i<N; ++i)cin >> x[i];for(int i = 0; i<N; ++i)cin >> y[i];double r;for(int i = 0; i<N; ++i)for(int j = i+1; j<N; ++j){r = distance(x[i], x[j], y[i], y[j]);edges.push_back(make_pair(r, make_pair(i, j)));}sort(edges.begin(), edges.end());int a, b;for(int i = 0; i<M; ++i){cin >> a >> b;sets.merge(a, b);}}double kruskal(DisjointSet& sets){int u, v;double ret = 0;double cost;for(int i = 0; i<edges.size(); ++i){cost = edges[i].first;u = edges[i].second.first;v = edges[i].second.second;if(sets.find(u) == sets.find(v)) continue;sets.merge(u, v);ret += cost;}return ret;}int main(){int C; cin >> C;cout << fixed;cout.precision(8);for(int tn = 0; tn < C; ++tn){cin >> N >> M;DisjointSet sets(N);init(sets);cout << kruskal(sets) << "\n";}}
cs https://algospot.com/judge/problem/read/LAN
algospot.com :: LAN
근거리 네트워크 문제 정보 문제 근거리 네트워크(LAN, Local Area Network)는 가정, 학교, 혹은 회사 등의 제한된 공간 내의 컴퓨터들을 연결하는 네트워크입니다. 알고스팟 대학교에서는 캠퍼스의 일부 건물들만이 서로 근거리 네트워크로 연결되어 있었는데, 이번에 캠퍼스 정보화 사업의 일환으로 모든 건물을 모두 연결하려고 합니다. 모든 건물이 서로 연결되어 있다는 것은 건물 사이의 케이블을 이용해 모든 건물 간에 서로 직접/간접적으로 데이터를
algospot.com
LAN
기본적인 MST문제
'Algorithm > 알고스팟' 카테고리의 다른 글
algospot - wildcard (0) 2020.02.02 algospot - jumpgame (0) 2020.02.02 algospot - promises (0) 2020.01.29 algospot - drunken (0) 2020.01.29 algospot - nthlon (0) 2020.01.20