Skip to content

Commit 53655ea

Browse files
authored
Merge pull request #367 from AlgorithmWithGod/khj20006
[20250617] BOJ / P3 / 물류창고 / 권혁준
2 parents 99909cd + da9b4ba commit 53655ea

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N, K, M;
57+
static long[] ans;
58+
static List<int[]> Edges;
59+
static TreeMap<Integer, Long>[] S;
60+
static int[] r;
61+
62+
static int f(int x) { return x==r[x] ? x : (r[x]=f(r[x])); }
63+
64+
public static void main(String[] args) throws Exception {
65+
66+
io = new IOController();
67+
68+
init();
69+
solve();
70+
71+
io.close();
72+
73+
}
74+
75+
public static void init() throws Exception {
76+
77+
N = io.nextInt();
78+
K = io.nextInt();
79+
M = io.nextInt();
80+
81+
r = new int[N+1];
82+
S = new TreeMap[N+1];
83+
for(int i=1;i<=N;i++) {
84+
S[i] = new TreeMap<>();
85+
S[i].put(io.nextInt(), 1L);
86+
r[i] = i;
87+
}
88+
89+
Edges = new ArrayList<>();
90+
for(int i=0;i<M;i++) {
91+
int a = io.nextInt(), b = io.nextInt(), c = io.nextInt();
92+
Edges.add(new int[]{a,b,c});
93+
}
94+
95+
ans = new long[K+1];
96+
97+
}
98+
99+
static void solve() throws Exception {
100+
101+
Collections.sort(Edges, (a,b) -> b[2]-a[2]);
102+
for(int[] e:Edges) {
103+
int a = e[0], b = e[1], c = e[2];
104+
105+
int x = f(a), y = f(b);
106+
if(x == y) continue;
107+
int small = x, large = y;
108+
if(S[x].size() > S[y].size()){
109+
small = y;
110+
large = x;
111+
}
112+
113+
for(int key : S[small].keySet()) {
114+
ans[key] += S[large].getOrDefault(key, 0L) * S[small].get(key) * c;
115+
S[large].put(key, S[large].getOrDefault(key, 0L) + S[small].get(key));
116+
}
117+
S[small] = new TreeMap<>();
118+
r[small] = large;
119+
120+
}
121+
122+
for(int i=1;i<=K;i++) io.write(ans[i] + "\n");
123+
124+
}
125+
126+
}
127+
```

0 commit comments

Comments
 (0)