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
- 알고리즘
- COSPRO
- java
- 재귀함수
- 백준코딩테스트
- 구현
- dp
- YBMCOS
- spring
- 다이나믹프로그래밍
- PS
- DFS
- 엘라스틱서치
- 게더타운시작
- 01BFS
- GatherTown
- 다익스트라
- deque
- 백준
- 이젠 골드구현도 어렵네..
- BFS
- 자바PS
- 취득후기
- 네트워크플로우
- 시뮬레이션
- 우선순위큐
- COSPROJAVA1급
- 세그먼트트리
- 완전탐색
- QUICKSTARTGUIDE
Archives
- Today
- Total
공부공간
BOJ - 6497 ) 전력난 본문
https://www.acmicpc.net/problem/6497
6497번: 전력난
문제 성진이는 한 도시의 시장인데 거지라서 전력난에 끙끙댄다. 그래서 모든 길마다 원래 켜져 있던 가로등 중 일부를 소등하기로 하였다. 길의 가로등을 켜 두면 하루에 길의 미터 수만큼 돈��
www.acmicpc.net
모든 가로등의 합에서 최소의 가로등의 비용으로 모든 노드를 연결해야하기때문에
MST문제와 동치이다. 크루스칼 알고리즘으로 해결하였다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class 전력난 {
public static void make() {
for(int i = 0 ; i < parent.length ; i++) {
parent[i] = i;
}
}
public static int findp(int x) {
if(parent[x] == x) return x;
else return findp(parent[x]);
}
public static boolean union(int x , int y) {
x = findp(x); y = findp(y);
if( x==y )return false;
if(x < y) {parent[y] = x;}
else parent[x] = y;
return true;
}
public static int[] parent;
public static class node{
int s ,e ,c;
public node(int s, int e, int c) {
super();
this.s = s;
this.e = e;
this.c = c;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb =new StringBuilder();
while(true) {
PriorityQueue<node> pq = new PriorityQueue<>(new Comparator<node>() {
@Override
public int compare(node o1, node o2) {
return o1.c - o2.c;
}
});
StringTokenizer st = new StringTokenizer(br.readLine());
int n =Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
if(n==0 && k==0) break;
int total=0 , answer =0;
parent = new int[n]; make();
for(int i = 0 ; i < k ; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
pq.add(new node(s,e,c));
total +=c;
}
int time = 1;
while(time != n) {
node now = pq.poll();
if(union(now.s , now.e)) {
time++;
answer +=now.c;
}
}
sb.append(total - answer + "\n");
}
System.out.println(sb);
}
}
'알고리즘 > 구현,시뮬' 카테고리의 다른 글
BOJ - 18111 ) 마인크래프트 (0) | 2020.07.22 |
---|---|
BOJ - 2887 ) 행성 터널 (0) | 2020.07.07 |
BOJ - 1647 ) 도시분할계획 (0) | 2020.07.07 |
BOJ - 2230 ) 수 고르기 (0) | 2020.06.29 |
BOJ - 1806 ) 부분합 (0) | 2020.05.11 |
Comments