https://www.acmicpc.net/problem/17472
17472번: 다리 만들기 2
첫째 줄에 지도의 세로 크기 N과 가로 크기 M이 주어진다. 둘째 줄부터 N개의 줄에 지도의 정보가 주어진다. 각 줄은 M개의 수로 이루어져 있으며, 수는 0 또는 1이다. 0은 바다, 1은 땅을 의미한다.
www.acmicpc.net
최소 스패닝 트리 문제이다.
일단 bfs로 섬을 마킹한 후, 섬에서 섬까지의 다리를 에지로 만들어
최소 스패닝 트리를 만들면 된다.
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
int map[101][101];
bool visited[101][101];
vector<int> parent;
int n, m, island;
vector<vector<pair<int, int>>> sumlist;
vector<pair<int, int>> mlist;
typedef struct edge
{
int s, e, v;
bool operator > (const edge& tmp) const {
return v > tmp.v;
}
} edge;
priority_queue<edge, vector<edge>, greater<edge>> pq;
int find(int a)
{
if (a == parent[a])
return a;
else
return parent[a] = find(parent[a]);
}
void munion(int a, int b)
{
a = find(a);
b = find(b);
if (a != b)
parent[b] = a;
}
void bfs(int i, int j)
{
queue<pair<int, int>> q;
q.push(make_pair(i, j));
mlist.push_back(make_pair(i, j));
visited[i][j] = true;
map[i][j] = island;
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int k = 0; k < 4; k++)
{
int nx = x + dx[k];
int ny = y + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if (!visited[nx][ny] && map[nx][ny] != 0)
{
visited[nx][ny] = true;
map[nx][ny] = island;
mlist.push_back(make_pair(nx, ny));
q.push(make_pair(nx, ny));
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
}
}
island = 1;
// 각 자리에서 bfs를 이용해 섬을 분리
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] != 0 && visited[i][j] != true)
{
bfs(i, j);
island++;
sumlist.push_back(mlist);
}
}
}
// 섬의 각 지점에서 만들 수 있는 모든 에지를 저장
for (int i = 0; i < sumlist.size(); i++)
{
vector<pair<int, int>> now = sumlist[i];
for (int j = 0; j < now.size(); j++)
{
int x = now[j].first;
int y = now[j].second;
int genzai = map[x][y];
for (int k = 0; k < 4; k++)
{
int blength = 0;
int nx = x + dx[k];
int ny = y + dy[k];
while (nx >= 0 && nx < n && ny >= 0 && ny < m)
{
// 같은 섬이면 에지를 만들 수 없음
if (map[nx][ny] == genzai)
break;
// 다른 섬이면 길이가 1이상일 때 에지 가능
else if (map[nx][ny] != 0)
{
if (blength > 1)
pq.push(edge{ genzai, map[nx][ny], blength });
break;
}
// 바다면 다리 길이 연장
else
blength++;
nx += dx[k];
ny += dy[k];
}
}
}
}
parent.resize(island);
for (int i = 0; i < parent.size(); i++)
{
parent[i] = i;
}
int used = 0;
int ans = 0;
while (!pq.empty())
{
edge now = pq.top();
pq.pop();
// 같은 부모가 아니라면 연결 가능
if (find(now.s) != find(now.e))
{
munion(now.s, now.e);
ans += now.v;
used++;
}
}
// 현재 island 값은 섬의 개수보다 1 많은 상태
if (used == island - 2)
{
cout << ans << '\n';
}
else
{
cout << -1 << '\n';
}
return 0;
}
|
cs |