

๋ฌธ์ ์ถ์ฒ : https://www.acmicpc.net/problem/2887
<์ ๊ทผ ๋ฐฉ๋ฒ>
1. ๋ชจ๋ ํ์ฑ์ ํฐ๋๋ก ์ฐ๊ฒฐํ๋๋ฐ ํ์ํ ์ต์ ๋น์ฉ์ ๊ตฌํด์ผ ํจ -> MST (Kruscal Algorithm ์ด์ฉ)
2. Kruscal์ ์ด์ฉํ๋ ค๋ฉด ๊ฐ์ ์ด ํ์ -> min(|Xa-Xb|, |Ya-Yb|, |Za-Zb|)๋ก ์ค์
But ํ์ฑ์ ๊ฐฏ์๊ฐ ์ต๋ 10๋ง๊ฐ์ด๊ธฐ ๋๋ฌธ์ ๋ชจ๋ ๊ฐ์ ์ ๊ตฌํํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ๋ฐ์ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ์๋๋๋ผ๋ ์๊ฐ์ด๊ณผ ๋ ์๋ ์์
3. ์ฐ๋ฆฌ๊ฐ ํ์ํ ๊ฐ์ ์ ๋น์ฉ์ด ์ต์์ธ ๊ฐ์ ์ด๊ธฐ ๋๋ฌธ์ ๊ฐ x,y,z์ ๋ํ์ฌ ๊ฐ์ฅ ๊ฐ๊น์ด ์ฆ ์ต์ ๋น์ฉ์ ๊ฐ์ง๋ ๊ฐ์ ๋ค๋ง ์ฐพ์์ ๋ฒกํฐ์ ํธ์ฌ์ฌ์ ๋์จ ํ์ธ๋๋ฅผ ํ์ฌ ์ต์ ์คํจ๋ ํธ๋ฆฌ ์ฐพ๊ธฐ
<ํ์ด>
1. ๊ฐ ํ์ฑ์ ๋ฒํธ, x, y, z๊ฐ์ ๋ชจ๋ ๋ฃ๊ธฐ ์ํด Pos๊ตฌ์กฐ์ฒด ์์ฑ
2. ๋ฒกํฐ ์์ฑ ํ input๊ฐ ๋ฐ์์ค๊ธฐ
3. x,y,x ๊ฐ๊ฐ์ ๋ํ์ฌ ์ ๋ ฌ ํ push
4. Kruscal Algorithm ์ด์ฉ
<์์ค ์ฝ๋>
|
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#define MAX 100000
using namespace std;
struct Pos{
int num,x,y,z;
};
int N, Parent[MAX], result=0;
vector<Pos> p_vec;
vector<pair<int,pair<int,int>>> Vec;
bool comp1(Pos &a, Pos &b){
if(a.x<b.x) return true;
return false;
}
bool comp2(Pos &a, Pos &b){
if(a.y<b.y) return true;
return false;
}
bool comp3(Pos &a, Pos &b){
if(a.z<b.z) return true;
return false;
}
void Sort(){
sort(p_vec.begin(),p_vec.end(),comp1);
for(int i=0; i<N-1; i++){
int val = min(abs(p_vec[i].x-p_vec[i+1].x),abs(p_vec[i+1].x-p_vec[i].x));
Vec.push_back({val,{p_vec[i].num,p_vec[i+1].num}});
}
sort(p_vec.begin(),p_vec.end(),comp2);
for(int i=0; i<N-1; i++){
int val = min(abs(p_vec[i].y-p_vec[i+1].y),abs(p_vec[i+1].y-p_vec[i].y));
Vec.push_back({val,{p_vec[i].num,p_vec[i+1].num}});
}
sort(p_vec.begin(),p_vec.end(),comp3);
for(int i=0; i<N-1; i++){
int val = min(abs(p_vec[i].z-p_vec[i+1].z),abs(p_vec[i+1].z-p_vec[i].z));
Vec.push_back({val,{p_vec[i].num,p_vec[i+1].num}});
}
}
int Find(int x){
if(Parent[x]==x) return x;
else return Parent[x] = Find(Parent[x]);
}
bool Same(int x, int y){
if(Find(x)==Find(y)) return true;
else return false;
}
int main() {
cin >> N;
for(int i=0; i<N; i++){
int x,y,z;
cin >> x >> y >> z;
p_vec.push_back({i,x,y,z});
Parent[i] = i;
}
Sort();
sort(Vec.begin(),Vec.end());
for(int i=0; i<Vec.size(); i++){
int from = Vec[i].second.first;
int to = Vec[i].second.second;
int cost = Vec[i].first;
if(Same(from,to)) continue;
result += cost;
Parent[Find(from)] = Find(to);
}
cout << result;
}
|
cs |
์ด๊ฒ ์ ๊ฐ ๊ตฌํํ ์ฝ๋๊ณ ๋ต์ ๋ง์ถ ํ ๋ค๋ฅธ ์ฌ๋๋ค์ ์ด๋ป๊ฒ ๊ตฌํํ๋ ํ ๋ฒ ๋ณด๊ณ ์ฐธ๊ณ ํด์ ๋ค์ ๊ตฌํํด ๋ดค์ต๋๋ค.
<์์ ํ ์์ค ์ฝ๋>
|
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#include <iostream>
#include <vector>
#include <algorithm>
#define MAX 100000
using namespace std;
int N, Parent[MAX], result=0;
vector<pair<int,int>> X;
vector<pair<int,int>> Y;
vector<pair<int,int>> Z;
vector<pair<int,pair<int,int>>> Vec;
int Find(int x){
if(Parent[x]==x) return x;
else return Parent[x] = Find(Parent[x]);
}
bool Same(int x, int y){
if(Find(x)==Find(y)) return true;
else return false;
}
int main() {
cin >> N;
for(int i=0; i<N; i++){
int x,y,z;
cin >> x >> y >> z;
X.push_back({x,i});
Y.push_back({y,i});
Z.push_back({z,i});
Parent[i] = i;
}
sort(X.begin(),X.end());
sort(Y.begin(),Y.end());
sort(Z.begin(),Z.end());
for(int i=0; i<N-1; i++){
Vec.push_back({X[i+1].first-X[i].first,{X[i].second,X[i+1].second}});
Vec.push_back({Y[i+1].first-Y[i].first,{Y[i].second,Y[i+1].second}});
Vec.push_back({Z[i+1].first-Z[i].first,{Z[i].second,Z[i+1].second}});
}
sort(Vec.begin(), Vec.end());
for(int i=0; i<Vec.size(); i++){
int from = Vec[i].second.first;
int to = Vec[i].second.second;
int cost = Vec[i].first;
if(Same(from,to)) continue;
result += cost;
Parent[Find(from)] = Find(to);
}
cout << result;
}
|
cs |

๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ฅผ ๋ณด๊ณ ๋๋๊ฑด '์ ์ ๋ด๊ฐ ๊ตณ์ด ํ๋์ ๋ฒกํฐ๋ก ๋ชจ๋ ํด๊ฒฐํ๋ ค๊ณ ํ์๊น' ์์ต๋๋ค.
x, y, z ๊ฐ๊ฐ์ ๋ํ ๋ฒกํฐ๋ฅผ ๋ง๋ค์ด์ ํ๋ ํจ์ฌ ์ฝ๋๋ ๊น๋ํ๊ณ ์ดํดํ๊ธฐ ์ฌ์๋ณด์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ ๋ ฌ์ ํด์คฌ๊ธฐ ๋๋ฌธ์ ๊ตณ์ด min๊ณผ abs๋ฅผ ์ธ ํ์๊ฐ ์์์ต๋๋ค. ๋ฉ๋ชจ๋ฆฌ์ ์๊ฐ ๋ชจ๋ ํฌ๊ฒ ์ฐจ์ด๊ฐ ๋์ง๋ ์์ง๋ง ์๋์ฝ๋๊ฐ ์ข ๋ ์๊ฐ์ ์ผ๋ก ๋ณด๊ธฐ ์ข๊ณ ์ดํดํ๊ธฐ ์ฌ์ด ์ฝ๋๋ผ๊ณ ์๊ฐํฉ๋๋ค.
ํ ๋ฒ์ ํด๊ฒฐํด์ ๋ฟ๋ฏํ๋๋ฐ ๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ฅผ ๋ณด๋ ์ญ์ ์์ง ๊ฐ๊ธธ์ด ๋จผ ๊ฒ ๊ฐ์ต๋๋ค..
'๐งโ๐ป ์ฝ๋ฉ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ฐฑ์ค 1644 - ์์์ ์ฐ์ํฉ (c++) (0) | 2022.08.19 |
|---|---|
| ๋ฐฑ์ค 2003 - ์๋ค์ ํฉ 2 (c++) (0) | 2022.08.09 |
| ๋ฐฑ์ค 4781 - ์ฌํ ๊ฐ๊ฒ (c++) (0) | 2022.08.05 |
| ๋ฐฑ์ค 12865 - ํ๋ฒํ ๋ฐฐ๋ญ (c++) (0) | 2022.08.01 |
| ๋ฐฑ์ค 15990 - 1, 2, 3 ๋ํ๊ธฐ 5 (c++) (0) | 2022.07.27 |