코딩테스트/백준_골드 [C++][백준 1922] 네트워크 연결 - [문제] 1922번: 네트워크 연결 (acmicpc.net) 1922번: 네트워크 연결 이 경우에 1-3, 2-3, 3-4, 4-5, 4-6을 연결하면 주어진 output이 나오게 된다. www.acmicpc.net [문제 풀이] 크루스칼 알고리즘의 기본 스탠다드 문제이다. 크루스칼 알고리즘을 알고 있다면 바로 풀 수 있는 문제. [회고] 특별히 없음. [코드] #include <iostream> #include <algorithm> #include <vector> using namespace std; vector<pair<int,pair<int,int> > > graph; int parent[1011]; int n,m; int answer; int find(int x){ if(parent[x] == x){ return x; } return parent[x] = find(parent[x]); } void mergeUnion(int x, int y){ int a = find(x); int b = find(y); if(a == b){ return; } parent[b] = a; return; } bool sameParent(int x, int y){ int a = find(x); int b = find(y); if(a == b){ return true; } return false; } void solve(){ for(int i = 0;i<m;i++){ if(sameParent(graph[i].second.first,graph[i].second.second) == false){ mergeUnion(graph[i].second.first,graph[i].second.second); answer += graph[i].first; } } } int main(){ cin>>n>>m; for(int i = 0;i<m;i++){ int cost,destination,source; cin>>source>>destination>>cost; graph.push_back(make_pair(cost,make_pair(source,destination))); } for(int i = 1;i<=n;i++){ parent[i] = i; } sort(graph.begin(),graph.end()); solve(); cout<<answer<<endl; return 0; } 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기Yoon-1212 Contents 당신이 좋아할만한 콘텐츠 [C++][백준 13460] 구슬 탈출 2 2023.04.14 [C++][백준 2056] 작업 2023.04.06 [C++] 도시 분할 계획 2023.04.05 [C++][백준 10026] 적록색약 2023.04.04 댓글 0 + 이전 댓글 더보기