-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCityDividingPlan
More file actions
100 lines (91 loc) · 2.27 KB
/
CityDividingPlan
File metadata and controls
100 lines (91 loc) · 2.27 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* 백준 #1647
*/
public class Main {
public static void main(String[] args){
new sol();
}
}
class sol{
Buf buf = new Buf();
int N, M;
public sol() {
N = buf.getInt();
M = buf.getInt();
House[] houses = new House[N];
for (int i = 0; i<N; i++){
houses[i] = new House();
}
for (int i = 0; i<M; i++){
int s = buf.getInt()-1;
int e = buf.getInt()-1;
int w = buf.getInt();
Road roadA = new Road(e, w);
Road roadB = new Road(s, w);
houses[s].addLink(roadA);
houses[e].addLink(roadB);
}
int result = 0;
int max = 0;
PriorityQueue<Road> roadq = new PriorityQueue<>(Comparator.comparingInt(Road::getW));
roadq.addAll(houses[0].getLink());
boolean[] isVisited = new boolean[N];
isVisited[0] = true;
while(!roadq.isEmpty()){
Road road = roadq.poll();
if (isVisited[road.getE()])continue;
isVisited[road.getE()] = true;
roadq.addAll(houses[road.getE()].getLink());
result+=road.getW();
max=Math.max(road.getW(), max);
}
System.out.println(result-max);
}
}
class Road {
private int e;
private int w;
public Road( int e, int w) {
this.e = e;
this.w = w;
}
public int getE() {
return e;
}
public int getW() {
return w;
}
}
class House{
private final ArrayList<Road> link = new ArrayList<>();
public void addLink(Road l){
this.link.add(l);
}
public ArrayList<Road> getLink() {
return link;
}
}
class Buf{
BufferedReader br;
StringTokenizer st;
Buf() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String get(){
while(st==null||!st.hasMoreElements()){
try{
st= new StringTokenizer(br.readLine());
}catch(IOException e){
return null;
}
}
return st.nextToken();
}
public Integer getInt(){
return Integer.parseInt(get());
}
}