Skip to content

Commit 5013a40

Browse files
authored
Merge pull request #810 from AlgorithmWithGod/0224LJH
[20250903] BOJ / G1 / 인터넷 설치 / 이종환
2 parents 9ef7ac8 + 77a5c1f commit 5013a40

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
```java
2+
import java.io.IOException;
3+
import java.io.*;
4+
import java.util.*;
5+
6+
7+
public class Main {
8+
9+
10+
static Node[] nodes;
11+
static boolean[] visited;
12+
static int[] totalCost;
13+
14+
static boolean canReach = false;
15+
static int nodeCnt, edgeCnt, freeCnt,ans;
16+
17+
static PriorityQueue<Node> nodePq = new PriorityQueue<>();;
18+
19+
static class Node implements Comparable<Node>{
20+
int num;
21+
HashMap<Integer,Integer> to = new HashMap<>();
22+
23+
public void addEdge(int nodeNum, int cost) {
24+
Node n = nodes[nodeNum];
25+
26+
if (!to.containsKey(nodeNum)) {
27+
to.put(nodeNum, cost);
28+
n.to.put(num, cost);
29+
return;
30+
}
31+
32+
if (to.get(nodeNum) > cost) {
33+
to.replace(nodeNum, cost);
34+
n.to.replace(num, cost);
35+
}
36+
}
37+
38+
public Node(int num) {
39+
this.num = num;
40+
}
41+
42+
@Override
43+
public int compareTo(Node o) {
44+
45+
return Integer.compare(totalCost[this.num], totalCost[o.num]);
46+
}
47+
}
48+
49+
50+
public static void main(String[] args) throws IOException {
51+
init();
52+
process();
53+
print();
54+
55+
}
56+
57+
public static void init() throws IOException {
58+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
59+
StringTokenizer st = new StringTokenizer(br.readLine());
60+
61+
nodeCnt = Integer.parseInt(st.nextToken());
62+
edgeCnt = Integer.parseInt(st.nextToken());
63+
freeCnt = Integer.parseInt(st.nextToken());
64+
65+
nodes = new Node[nodeCnt+1];
66+
visited = new boolean[nodeCnt+1];
67+
totalCost = new int [nodeCnt+1];
68+
69+
for (int i = 1; i <= nodeCnt; i++) {
70+
nodes[i] = new Node(i);
71+
}
72+
73+
for (int i = 0; i < edgeCnt; i++) {
74+
st = new StringTokenizer(br.readLine());
75+
int num1 = Integer.parseInt(st.nextToken());
76+
int num2 = Integer.parseInt(st.nextToken());
77+
int cost = Integer.parseInt(st.nextToken());
78+
79+
nodes[num1].addEdge(num2, cost);
80+
}
81+
82+
83+
}
84+
85+
public static void process() throws IOException {
86+
87+
88+
int start = 0;
89+
int end = 1000000;
90+
91+
int mid = (start+end)/2;
92+
93+
int lowestSuccess = Integer.MAX_VALUE;
94+
95+
while(start < end) {
96+
canReach = false;
97+
djikstra(mid);
98+
System.out.println(mid);
99+
if (canReach) {
100+
end = mid;
101+
102+
lowestSuccess = Math.min(mid,lowestSuccess);
103+
}
104+
else start = mid+1;
105+
106+
mid = (start+end)/2;
107+
108+
109+
}
110+
111+
if (lowestSuccess == Integer.MAX_VALUE) {
112+
ans = -1;
113+
} else ans = lowestSuccess;
114+
115+
}
116+
117+
118+
119+
120+
121+
122+
private static void djikstra(int limit) {
123+
Arrays.fill(totalCost, Integer.MAX_VALUE/2);
124+
Arrays.fill(visited, false);
125+
totalCost[1] = 0;
126+
nodePq.add(nodes[1]);
127+
128+
129+
while(!nodePq.isEmpty()) {
130+
Node node = nodePq.poll();
131+
if (visited[node.num]) continue;
132+
133+
visited[node.num] = true;
134+
135+
for (int n: node.to.keySet()) {
136+
int cost = node.to.get(n);
137+
138+
int temp = totalCost[node.num];
139+
if (cost > limit) temp++;
140+
141+
if (temp > freeCnt) continue;
142+
143+
if (totalCost[n] > temp) {
144+
totalCost[n] = temp;
145+
nodePq.add(nodes[n]);
146+
}
147+
}
148+
149+
150+
}
151+
152+
if (totalCost[nodeCnt] != Integer.MAX_VALUE/2) canReach = true;
153+
154+
}
155+
156+
public static void print() {
157+
System.out.println(ans);
158+
}
159+
}
160+
161+
```

0 commit comments

Comments
 (0)