Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions 0224LJH/202509/03 BOJ 인터넷 설치
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
```java
import java.io.IOException;
import java.io.*;
import java.util.*;


public class Main {


static Node[] nodes;
static boolean[] visited;
static int[] totalCost;

static boolean canReach = false;
static int nodeCnt, edgeCnt, freeCnt,ans;

static PriorityQueue<Node> nodePq = new PriorityQueue<>();;

static class Node implements Comparable<Node>{
int num;
HashMap<Integer,Integer> to = new HashMap<>();

public void addEdge(int nodeNum, int cost) {
Node n = nodes[nodeNum];

if (!to.containsKey(nodeNum)) {
to.put(nodeNum, cost);
n.to.put(num, cost);
return;
}

if (to.get(nodeNum) > cost) {
to.replace(nodeNum, cost);
n.to.replace(num, cost);
}
}

public Node(int num) {
this.num = num;
}

@Override
public int compareTo(Node o) {

return Integer.compare(totalCost[this.num], totalCost[o.num]);
}
}


public static void main(String[] args) throws IOException {
init();
process();
print();

}

public static void init() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

nodeCnt = Integer.parseInt(st.nextToken());
edgeCnt = Integer.parseInt(st.nextToken());
freeCnt = Integer.parseInt(st.nextToken());

nodes = new Node[nodeCnt+1];
visited = new boolean[nodeCnt+1];
totalCost = new int [nodeCnt+1];

for (int i = 1; i <= nodeCnt; i++) {
nodes[i] = new Node(i);
}

for (int i = 0; i < edgeCnt; i++) {
st = new StringTokenizer(br.readLine());
int num1 = Integer.parseInt(st.nextToken());
int num2 = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());

nodes[num1].addEdge(num2, cost);
}


}

public static void process() throws IOException {


int start = 0;
int end = 1000000;

int mid = (start+end)/2;

int lowestSuccess = Integer.MAX_VALUE;

while(start < end) {
canReach = false;
djikstra(mid);
System.out.println(mid);
if (canReach) {
end = mid;

lowestSuccess = Math.min(mid,lowestSuccess);
}
else start = mid+1;

mid = (start+end)/2;


}

if (lowestSuccess == Integer.MAX_VALUE) {
ans = -1;
} else ans = lowestSuccess;

}






private static void djikstra(int limit) {
Arrays.fill(totalCost, Integer.MAX_VALUE/2);
Arrays.fill(visited, false);
totalCost[1] = 0;
nodePq.add(nodes[1]);


while(!nodePq.isEmpty()) {
Node node = nodePq.poll();
if (visited[node.num]) continue;

visited[node.num] = true;

for (int n: node.to.keySet()) {
int cost = node.to.get(n);

int temp = totalCost[node.num];
if (cost > limit) temp++;

if (temp > freeCnt) continue;

if (totalCost[n] > temp) {
totalCost[n] = temp;
nodePq.add(nodes[n]);
}
}


}

if (totalCost[nodeCnt] != Integer.MAX_VALUE/2) canReach = true;

}

public static void print() {
System.out.println(ans);
}
}

```