Skip to content

Commit 87b3fad

Browse files
authored
Merge pull request #49 from saechimdaeki/master
비용
2 parents 57f8ed9 + a214409 commit 87b3fad

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package 백준
2+
private val arr= arrayListOf<Triple<Int,Int,Int>>()
3+
private lateinit var parent:IntArray
4+
private lateinit var size:LongArray
5+
fun main() {
6+
val br=System.`in`.bufferedReader()
7+
val (n,m)=br.readLine().split(" ").map { it.toInt() }
8+
parent=IntArray(n+1){it}
9+
size= LongArray(n+1){1}
10+
var costsum:Long=0
11+
repeat(m) {
12+
val (a,b,c)=br.readLine().split(" ").map { it.toInt() }
13+
arr.add(Triple(a,b,c))
14+
costsum+=c
15+
}
16+
arr.sortByDescending{ it.third }
17+
var ans:Long=0
18+
for(i in arr){
19+
if(find(i.first)!= find(i.second)){
20+
ans+=((((size[find(i.first)]* size[find(i.second)])%1000000000)*costsum))
21+
ans%=1000000000
22+
union(i.first,i.second)
23+
}
24+
costsum-=i.third
25+
}
26+
println(ans)
27+
}
28+
private fun find(x:Int):Int{
29+
return when (x) {
30+
parent[x] -> {
31+
x
32+
}
33+
else -> {
34+
parent[x] = find(parent[x])
35+
parent[x]
36+
}
37+
}
38+
}
39+
private fun union(x:Int,y:Int){
40+
val (px,py)= arrayOf(find(x), find(y))
41+
when {
42+
px>py -> {
43+
parent[px]=py
44+
size[py]+= size[px]
45+
size[px]=1
46+
}
47+
else -> {
48+
parent[py]=px
49+
size[px]+= size[py]
50+
size[py]=1
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)