반응형
문제 출처 :
https://www.acmicpc.net/problem/1197
알고리즘 분석 :
문제 해결에 필요한 사항
1. 유니온 파인드(Union Find) :: http://www.crocus.co.kr/683
2. 최소 스패닝 트리(MST) :: http://www.crocus.co.kr/733
이 문제는 최소 스패닝 트리 문제로 Kruskal Algorithm을 이용하면 해결 할 수 있다.
크루스칼 알고리즘은 유니온 파인드 방식과 상당히 유사한데,
간선의 정보만 오름차순으로 업데이트를 한번 해주면 그 이후로는 유니온 파인드 방식으로 해결 할 수 있다.
소스 코드 :
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 | #include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; typedef struct kruskal { int from; int to; int val; }KS; vector<KS> edge; int chk[10002]; int res; bool comp(KS d1, KS d2) { return d1.val < d2.val; } // 유니온 파인드의 파인드 방식 int find(int num) { if (num == chk[num]) return num; return chk[num] = find(chk[num]); } int main() { int V, E; scanf("%d %d", &V, &E); for (int i = 1; i <= V; i++) chk[i] = i; // 그래프 형성 for (int i = 0; i < E; i++) { KS ks; scanf("%d %d %d", &ks.from, &ks.to, &ks.val); edge.push_back(ks); } // 크루스칼 알고리즘에 의해 간선을 오름차순 정렬 sort(edge.begin(), edge.end(), comp); // 유니온 파인드의 유니온 방식 for (int i = 0; i < E; i++) { int nv = find(edge[i].from); int nw = find(edge[i].to); if (nv == nw) continue; chk[nv] = nw; res += edge[i].val; } printf("%d", res); return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[4343번] Arctic Network (0) | 2017.04.04 |
---|---|
[1922번] 네트워크 연결 (0) | 2017.04.03 |
[2529번] 부등호 (0) | 2017.04.02 |
[3665번] 최종 순위 (5) | 2017.04.02 |
[1005번] ACM Craft (5) | 2017.04.02 |