[Programmers] 섬 연결하기

Time Lapse :58min 32sec

solution.cpp

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
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
class edge{
public:
int from;
int to;
int value;
edge(int f, int t, int v) : from(f) , to(t), value(v){}
};
class cmp{
public:
bool operator()(edge e1, edge e2){
return e1.value>e2.value;
}
};

int solution(int n, vector<vector<int>> costs) {
int answer = 0;
int map[101][101];
bool visit[101];
int min_from;
int min_to;
int min_value = 987654321;
for (int i = 0; i < costs.size(); i++) {
map[costs[i][0]][costs[i][1]] = costs[i][2];
map[costs[i][1]][costs[i][0]] = costs[i][2];
if (min_value > costs[i][2]) {
min_value = costs[i][2];
min_from = costs[i][0];
min_to = costs[i][1];
}
}
priority_queue<edge, vector<edge>, cmp> pq;
for(int i=0;i<n;i++){
if(map[min_from][i]!=0){
pq.push(edge(min_from,i,map[min_from][i]));
}
if(map[min_to][i]!=0){
pq.push(edge(min_to,i,map[min_to][i]));
}
}
visit[min_from] = true;
visit[min_to] = true;
answer += min_value;
int cnt = 2;
while (!pq.empty()) {
int f = pq.top().from;
int t = pq.top().to;
int v = pq.top().value;
pq.pop();
if (visit[t] && visit[f])
continue;
answer += v;
if (!visit[t]) {
for (int i = 0; i < n; i++) {
if (map[t][i] != 0&&!visit[i]) {
pq.push(edge(t, i, map[t][i]));
}
}
visit[t] = true;
cnt++;
}
if (!visit[f]) {
for (int i = 0; i < n; i++) {
if (map[f][i] != 0&&!visit[i]) {
pq.push(edge(f, i, map[f][i]));
}
}
visit[f] = true;
cnt++;
}
if(cnt==n)
break;
}
return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/connectingIsland/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.