Recent Posts
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JanusWebRTCGateway
- kotlin
- Value too long for column
- JanusWebRTC
- 자원부족
- vfr video
- 깡돼후
- 티스토리챌린지
- Spring Batch
- table not found
- python
- 개성국밥
- mp4fpsmod
- PytestPluginManager
- 오블완
- terminal
- 코루틴 컨텍스트
- taint
- 겨울 부산
- k8s #kubernetes #쿠버네티스
- 헥사고날아키텍처 #육각형아키텍처 #유스케이스
- pytest
- JanusGateway
- PersistenceContext
- tolerated
- 달인막창
- VARCHAR (1)
- JanusWebRTCServer
- preemption #
- 코루틴 빌더
Archives
너와 나의 스토리
(BOJ) 1922 네트워크 연결 본문
반응형
문제: https://www.acmicpc.net/problem/1922
문제 풀이: mst 사용 ㅎㅎ
소스 코드:
typedef pair<int, pair<int, int>> P;
priority_queue<P, vector<P>, greater<P>> pq;
int n, m,cnt,p[1001],res;
int find(int x) {
if (p[x] < 0) return x;
return p[x] = find(p[x]);
}
bool merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return false;
p[a] = b;
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n >> m;
memset(p, -1, sizeof(p));
for (int i = 0; i < m; i++) {
int q, w, e;
cin >> q >> w >> e;
pq.push({ e,{q,w} });
}
while (!pq.empty()) {
int cost = pq.top().first;
int a = pq.top().second.first;
int b = pq.top().second.second;
pq.pop();
if (merge(a, b)) {
cnt++;
res += cost;
if (cnt == n - 1) break;
}
}
cout << res<<"\n";
return 0;
}
반응형
'Algorithm > Minimum Spanning Tree' 카테고리의 다른 글
(BOJ) 4386 별자리 만들기 (0) | 2019.05.25 |
---|---|
(BOJ) 1774 우주신과의 교감 (4) | 2019.05.14 |
Comments