티스토리 뷰
1647번: 도시 분할 계획
첫째 줄에 집의 개수N, 길의 개수M이 주어진다. N은 2이상 100,000이하인 정수이고, M은 1이상 1,000,000이하인 정수이다. 그 다음 줄부터 M줄에 걸쳐 길의 정보가 A B C 세 개의 정수로 주어지는데 A번 집
www.acmicpc.net
백준 소스코드 [C++] 1647 도시 분할 계획
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
typedef long long ll;
using namespace std;
int n, m,u,v,w;
vector<pii> adj[100000];
bool checked[100000];
priority_queue<pii,vector<pii>,greater<pii>> pq;
vector<int> res;
vector<int> prim() {
checked[0] = true;
for (auto there : adj[0])
if (checked[there.second] == false)
pq.push(there);
while (pq.empty() != true) {
int there = pq.top().second;
int cost = pq.top().first;
pq.pop();
if (checked[there] == true) continue;
checked[there] = true;
res.push_back(cost);
for (auto there : adj[there])
if (checked[there.second] == false)
pq.push(there);
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0;i < m;i++) {
cin >> u >> v >> w;
adj[u - 1].push_back({w,v-1 });
adj[v - 1].push_back({w,u-1 });
}
vector<int> res = prim();
sort(all(res));
int ret = 0;
for (int i = 0;i < (int)res.size() - 1;i++)ret += res[i];
cout << ret;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 12865 평범한 배낭 (0) | 2020.12.10 |
---|---|
백준 소스코드 [C++] 14950 정복자 (0) | 2020.11.27 |
백준 소스코드 [C++] 11869 님블 (0) | 2020.11.26 |
백준 소스코드 [C++] 11868 님 게임 2 (0) | 2020.11.25 |
백준 소스코드 [C++] 14284 간선 이어가기 2 (0) | 2020.11.23 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 벨만포드 시간복잡도
- CPU와 Memory
- 최단경로 문제
- CompositionalLayout
- WWDC19
- 포드 풀커슨 알고리즘
- 네트워크 유량
- 벨만포드 알고리즘
- WWDC17
- 부스트캠프 6기
- HIG
- 컴퓨터 추상화
- 에드몬드 카프 알고리즘
- 코딩대회
- 최대 매칭
- mach-o
- 최단경로문제
- State Restoration
- Testable
- rxswift
- test coverage
- 최단경로 알고리즘
- observeOn
- WWDC16
- WWDC21
- 다익스트라 시간복잡도
- IOS
- 네트워크 플로우
- MeTal
- 강한 순환 참조
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함