You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
connected[1] = true; // 1번 노드부터 시작while(true) {
int min = 10001;
int k = -1, l = -1;
for(int i = 0; i<edges.size(); ++i) {
pair<int, int> e = edges[i];
if(!connected[e.first] && !connected[e.second]) continue; // 두 노드 중 연결된 노드가 하나도 없는 경우if(connected[e.first] && connected[e.second]) continue; // 두 노드가 이미 최소 스패닝 트리에 포함되어 있는 경우 (루프 발생 가능)if(costs[i] < min) { // 최소값 업데이트
min = costs[i];
k = e.first;
l = e.second;
}
}
if(min == 10001) break; // 아무 노드도 선택되지 않은 경우 = 모든 노드가 연결된 경우
ans += min;
connected[k] = true;
connected[l] = true;
}
The text was updated successfully, but these errors were encountered:
1922: 네트워크 연결
소스 코드
아이디어
Greedy의 일종인 prim 알고리즘을 통해 최소 스패닝 트리를 찾는다.
구현
The text was updated successfully, but these errors were encountered: