Skip to content

Commit da3fed7

Browse files
authored
[20250820] BOJ / G3/ 파티 / 이종환
1 parent 75742bf commit da3fed7

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

0224LJH/202508/20 BOJ 파티.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
``` java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.*;
6+
7+
8+
public class Main {
9+
static final int MAX = Integer.MAX_VALUE/2;
10+
11+
static PriorityQueue<Node> nodePq = new PriorityQueue<>();
12+
static PriorityQueue<Edge> edgePq = new PriorityQueue<>();
13+
static Node[] nodes;
14+
static boolean[] visited;
15+
static int[] dis,totalSum;
16+
17+
static int nodeCnt, edgeCnt, target, ans;
18+
19+
20+
21+
static class Node implements Comparable<Node>{
22+
int num;
23+
int dis;
24+
List<Edge> edges;
25+
26+
public Node(int num) {
27+
this.num = num;
28+
edges = new ArrayList<>();
29+
}
30+
31+
public int compareTo(Node n) {
32+
return this.dis - n.dis;
33+
}
34+
}
35+
36+
static class Edge implements Comparable<Edge>{
37+
int to;
38+
int weight;
39+
40+
public Edge(int to, int weight) {
41+
this.to = to;
42+
this.weight = weight;
43+
}
44+
45+
@Override
46+
public int compareTo(Edge e) {
47+
return this.weight - e.weight;
48+
}
49+
50+
51+
}
52+
53+
public static void main(String[] args) throws IOException {
54+
init();
55+
process();
56+
print();
57+
}
58+
59+
public static void init() throws IOException {
60+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
61+
StringTokenizer st = new StringTokenizer(br.readLine());
62+
63+
nodeCnt = Integer.parseInt(st.nextToken());
64+
edgeCnt = Integer.parseInt(st.nextToken());
65+
target = Integer.parseInt(st.nextToken());
66+
ans = 0;
67+
68+
nodes = new Node[nodeCnt+1];
69+
totalSum = new int[nodeCnt+1];
70+
for (int i = 1; i <= nodeCnt; i++) {
71+
nodes[i] = new Node(i);
72+
}
73+
74+
for (int i = 0; i < edgeCnt; i++) {
75+
st = new StringTokenizer(br.readLine());
76+
int from = Integer.parseInt(st.nextToken());
77+
int to = Integer.parseInt(st.nextToken());
78+
int weight = Integer.parseInt(st.nextToken());
79+
nodes[from].edges.add(new Edge(to,weight));
80+
}
81+
82+
}
83+
84+
public static void process() {
85+
for (int i = 1; i <= nodeCnt; i++) {
86+
makeMinDis(i);
87+
if (i == target) {
88+
for (int j = 1; j <= nodeCnt; j++) {
89+
totalSum[j] += dis[j];
90+
}
91+
92+
} else {
93+
totalSum[i] += dis[target];
94+
}
95+
}
96+
97+
for (int i = 1; i <= nodeCnt; i++) {
98+
ans = Math.max(ans,totalSum[i]);
99+
}
100+
}
101+
102+
103+
private static void makeMinDis(int start) {
104+
105+
dis = new int[nodeCnt+1];
106+
visited = new boolean[nodeCnt+1];
107+
Arrays.fill(dis, MAX);
108+
dis[start] = 0;
109+
110+
nodePq.clear();
111+
nodePq.add(nodes[start]);
112+
113+
while (!nodePq.isEmpty()) {
114+
Node n = nodePq.poll();
115+
int from = n.num;
116+
117+
if (from != start && from == target) break;
118+
119+
if (visited[from]) continue;
120+
visited[from] = true;
121+
122+
edgePq.clear();
123+
edgePq.addAll(n.edges);
124+
125+
while (!edgePq.isEmpty()) {
126+
Edge e = edgePq.poll();
127+
128+
if (visited[e.to]) continue;
129+
int newDis = dis[from] + e.weight;
130+
if (newDis < dis[e.to]) {
131+
dis[e.to] = newDis;
132+
nodes[e.to].dis = dis[e.to];
133+
nodePq.add(nodes[e.to]);
134+
}
135+
136+
}
137+
}
138+
139+
}
140+
141+
public static void print() {
142+
System.out.println(ans);
143+
}
144+
}
145+
```

0 commit comments

Comments
 (0)