Notice
Recent Posts
Recent Comments
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
- 백준 17471
- 백준 17822
- c#
- ㅣ풀이
- 조세퍼스 순열
- 버킷 정렬
- 해시구현
- 5397
- 시간 복잡도
- qorwns
- 구현
- Stack 이란
- 별 찍기 10
- 스택의 특징
- C/C++ 구현
- 백준
- 백준 17779
- AVL 시간 복잡도
- 자료구조
- dfs
- 원판 돌리기
- 백준 5397
- heap
- 풀이
- 백준 1406
- 해시 구현
- 백준 2447
- 1764
- 게리멘더링2
- 백준 1158
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 6497] 전력난 본문
분류 : 크루스칼
요구사항
기본적인 크루스칼 문제
풀이
최소 신장 트리의 합을 구하여 전체 합에서 빼준다
#include <stdio.h>
#include <map>
#include <algorithm>
//#include "Windows.h"
#include <iostream>
#include <vector>
#include <queue>
#include <list>
using namespace std;
struct go{
int x;
int y;
int z;
};
int parent[200200];
vector<go> dist;
int n,m;
int Find(int x){
if(parent[x]==x) return x;
return parent[x]=Find(parent[x]);
}
void Union(int x, int y){
x= Find(x);
y= Find(y);
if(x!=y) parent[x]=y;
}
bool com(go a, go b){
if(a.z < b.z) return true;
return false;
}
int main(){
while(true){
scanf("%d%d",&n,&m);
int sum=0;
if(n==0 && m==0) break;
for(int i=0 ; i < n ; i++) parent[i]=i;
dist.clear();
for(int i=0 ; i < m ; i++){
int x, y, z;
scanf("%d%d%d",&x,&y,&z);
dist.push_back({x,y,z});
dist.push_back({y,x,z});
sum+=z;
}
sort(dist.begin(),dist.end(),com);
int cnt=0;
int now=0;
int ans=0;
int distsize=dist.size();
while(now != distsize){
// cout<<now<<endl;
int nownode = dist[now].x;
int nextnode = dist[now].y;
int nowdist= dist[now].z;
now++;
if(Find(nownode) != Find(nextnode)){
Union(nownode, nextnode);
ans+=nowdist;
cnt++;
}
if(cnt==n-1) break;
}
//cout<<sum<<" "<<ans<<endl;
cout<<sum-ans<<endl;
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 4386] 별자리 만들기 (0) | 2020.03.23 |
---|---|
[백준 1647] 도시 분할 계획 (0) | 2020.03.23 |
[백준 1924] 2007년 (0) | 2020.03.20 |
[백준 2870] 수학숙제 (0) | 2020.03.20 |
[백준 1059] 수2 (0) | 2020.03.20 |
Comments