๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿง‘‍๐Ÿ’ป ์ฝ”๋”ฉ/Algorithm

๋ฐฑ์ค€ 2887 - ํ–‰์„ฑ ํ„ฐ๋„ (c++)

728x90

๋ฌธ์ œ ์ถœ์ฒ˜ : 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๋ฅผ ์“ธ ํ•„์š”๊ฐ€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„ ๋ชจ๋‘ ํฌ๊ฒŒ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€๋Š” ์•Š์ง€๋งŒ ์•„๋ž˜์ฝ”๋“œ๊ฐ€ ์ข€ ๋” ์‹œ๊ฐ์ ์œผ๋กœ ๋ณด๊ธฐ ์ข‹๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

 

ํ•œ ๋ฒˆ์— ํ•ด๊ฒฐํ•ด์„œ ๋ฟŒ๋“ฏํ–ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ์—ญ์‹œ ์•„์ง ๊ฐˆ๊ธธ์ด ๋จผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..

728x90