Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 6497번 전력난 (gold 4 본문
import java.io.*;
import java.util.*;
class Point{
int y;
int l;
public Point(int y, int l){
this.y=y;
this.l=l;
}
}
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
PriorityQueue<Point> que=new PriorityQueue<>(new Comparator<Point>(){
public int compare(Point o1, Point o2){
return o1.l-o2.l;
}
});
while(true) {
que.clear(); //까먹음. 비우지 않으면 전의 값들로 인해 index오류 발생
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.valueOf(st.nextToken());
int m = Integer.valueOf(st.nextToken());
if(n+m==0){
break;
}
ArrayList<Point>[] map = new ArrayList[n];
for (int i = 0; i < n; i++) {
map[i] = new ArrayList<>();
}
int answer=0;
for (int i=0; i<m; i++){
st=new StringTokenizer(br.readLine());
int x=Integer.valueOf(st.nextToken());
int y=Integer.valueOf(st.nextToken());
int l=Integer.valueOf(st.nextToken());
map[x].add(new Point(y,l));
map[y].add(new Point(x,l));
answer+=l;
}
boolean[] visit = new boolean[n];
int visitN=0;
que.offer(new Point(0,0));
while(!que.isEmpty()){
Point a=que.poll();
if(visit[a.y]){
continue;
}
visitN++;
answer-=a.l;
visit[a.y]=true;
if(visitN==n){
break;
}
for(int i=0; i<map[a.y].size(); i++){
Point temp=map[a.y].get(i);
if(visit[temp.y]){
continue;
}
que.offer(new Point(temp.y, temp.l));
}
}
bw.write(answer+"\n");
}
bw.flush();
}
}
'백준공부 > java' 카테고리의 다른 글
[백준] 4386번 별자리 만들기 (gold 3 (0) | 2023.12.19 |
---|---|
[백준] 30802번 웰컴 키드 (bronze 3 (0) | 2023.11.29 |
[백준] 30619번 내 집 마련하기(silver 2 (0) | 2023.11.13 |
[백준] 30617번 Knob (bronze 3 (0) | 2023.11.13 |
[백준] 30618번 donstructive (silver 4 (0) | 2023.11.13 |