Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준 17779
- dfs
- heap
- AVL 시간 복잡도
- 백준 17822
- 버킷 정렬
- 백준
- 구현
- 스택의 특징
- c#
- 풀이
- 백준 17471
- ㅣ풀이
- 게리멘더링2
- 1764
- C/C++ 구현
- 해시 구현
- 백준 5397
- 자료구조
- 원판 돌리기
- 백준 2447
- 5397
- 별 찍기 10
- 백준 1158
- 시간 복잡도
- qorwns
- 조세퍼스 순열
- 해시구현
- Stack 이란
- 백준 1406
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 4386] 별자리 만들기 본문
분류 : 크루스칼
요구사항
별자리를 이은 최소 신장 트리의 크기 구하기
풀이
각 별자리 간의 길이를 저장해준 뒤, 최소 신장 트리를 구한다
#include <stdio.h>
#include <map>
#include <algorithm>
//#include "Windows.h"
#include <iostream>
#include <vector>
#include <queue>
#include <list>
#include <math.h>
using namespace std;
struct go{
double x;
double y;
double z;
};
int parent[100001];
vector<go> dist;
vector<go> mydist;
int n,m;
int Find(int x){
if(parent[x]==x) return x;
return parent[x]=Find(parent[x]);
}
void Union(int x, int y){
x= Find(x);
y= Find(y);
if(x!=y) parent[x]=y;
}
bool com(go a, go b){
if(a.z < b.z) return true;
return false;
}
int main(){
scanf("%d",&m);
double sum=0;
for(int i=1 ; i <= m ; i++) parent[i]=i;
dist.clear();
for(int i=0 ; i < m ; i++){
double x, y;
cin>>x>>y;
dist.push_back({x,y});
}
int a=0;
for(double i=0 ; i < m ; i++){
for(double j=i+1; j <m ; j++){
double nowx = dist[i].x;
double nowy = dist[i].y;
double nextx= dist[j].x;
double nexty= dist[j].y;
double getdist = sqrt( abs(nowx-nextx) * abs(nowx-nextx) + abs(nowy-nexty) * abs(nowy-nexty) );
mydist.push_back({i,j,getdist});
a++;
}
}
sort(mydist.begin(),mydist.end(), com);
int cnt=0;
for(int i=0 ; i < a ; i++){
double x = mydist[i].x;
double y = mydist[i].y;
double ddist= mydist[i].z;
if(Find(x) != Find(y)){
Union(x,y);
sum+=ddist;
cnt++;
}
if(cnt==m-1) break;
}
//cout<<sum<<endl;
printf("%.2f",sum);
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 1719] 택배 (0) | 2020.03.24 |
---|---|
[백준 2343] 기타 레슨 (0) | 2020.03.23 |
[백준 1647] 도시 분할 계획 (0) | 2020.03.23 |
[백준 6497] 전력난 (0) | 2020.03.23 |
[백준 1924] 2007년 (0) | 2020.03.20 |
Comments