백준 알고리즘 4343번 : Arctic Network

Zoo Da·2021년 8월 24일
0

백준 알고리즘

목록 보기
180/337
post-thumbnail

링크

https://www.acmicpc.net/problem/4343

문제

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.

Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

입력

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

출력

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

예제 입력 및 출력

풀이 코드(C++)

#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define sz(a) int((a).size())
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using wector = vector<vector<int>>;
using tiii = tuple<int,int,int>;

struct UnionFind {
	vector<int> parent, rank, cnt;
	UnionFind(int n) : parent(n), rank(n, 1), cnt(n, 1) {
		iota(parent.begin(), parent.end(), 0);
	}
	int Find(int x) {
		return x == parent[x] ? x : parent[x] = Find(parent[x]);
	}
	bool Union(int a, int b) {
		a = Find(a), b = Find(b);
		if (a == b) return 0;
		if (rank[a] < rank[b]) swap(a, b);
		parent[b] = a;
		rank[a] += rank[a] == rank[b];
		cnt[a] += cnt[b];
		return 1;
	}
};

struct Edge{
  int u,v;
  double w;
  Edge(): Edge(-1, -1, 0){}
  Edge(int u1,int v1, double w1): u(u1),v(v1),w(w1){}
  bool operator < (const Edge& O)const{ return w < O.w; };
};

int main(){
  fastio;
  int tc; cin >> tc;
  while(tc--){
    int n,s; cin >> n >> s;
    vector<pii> coord(s);
    UnionFind UF(s);
    for(int i = 0; i < s; i++) cin >> coord[i].X >> coord[i].Y;
    vector<Edge> e;
    for(int i = 0; i < s - 1; i++){
      for(int j = i + 1; j < s; j++){
        double c = hypot(coord[i].X - coord[j].X,coord[i].Y - coord[j].Y);
        e.pb(Edge(i, j, c));
      }
    }
    sort(e.begin(), e.end());
    int cnt = 0;
    for(int i = 0; ; i++){
      if(UF.Union(e[i].u, e[i].v)){
        if(++cnt == s - n){
          cout << fixed << setprecision(2) << e[i].w << "\n";
          break;
        }
      }
    }
  }
  return 0;
}

s - 1개의 간선은 공짜로 연결이 되니 p - s개의 간선만 연결해주고 맨 뒤의 간선의 가중치를 출력해주면 됩니다.
cmath파일 안의 hypot함수를 활용하면 두 정점 사이의 거리를 간편하게 계산할 수 있습니다.
(hypot는 sqrt(a^2 - b^2)를 반환합니다.)

profile
메모장 겸 블로그

0개의 댓글