Skip to content

Commit bcfdeec

Browse files
authored
[백준 2307] 도로검문 - 최단경로
1 parent 71ca657 commit bcfdeec

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

hoseok/week79/Boj2307.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Main {
5+
6+
static class Node {
7+
int number, cost, edge;
8+
List<Integer> paths = new ArrayList<>();
9+
10+
public Node(int number, int cost) {
11+
this.number = number;
12+
this.cost = cost;
13+
}
14+
15+
public Node(int number, int cost, int edge) {
16+
this.number = number;
17+
this.cost = cost;
18+
this.edge = edge;
19+
}
20+
}
21+
22+
static int n, m;
23+
static List<Node>[] graph;
24+
static int[] edges;
25+
static int minDist;
26+
27+
public static void main(String[] args) throws Exception {
28+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
29+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
30+
StringTokenizer st = new StringTokenizer(br.readLine());
31+
n = Integer.parseInt(st.nextToken());
32+
m = Integer.parseInt(st.nextToken());
33+
34+
graph = new ArrayList[n + 1];
35+
edges = new int[m];
36+
37+
for (int i = 0; i <= n; i++) {
38+
graph[i] = new ArrayList<>();
39+
}
40+
41+
for (int i = 0; i < m; i++) {
42+
st = new StringTokenizer(br.readLine());
43+
int a = Integer.parseInt(st.nextToken());
44+
int b = Integer.parseInt(st.nextToken());
45+
int c = Integer.parseInt(st.nextToken());
46+
edges[i] = c;
47+
graph[a].add(new Node(b, c, i));
48+
graph[b].add(new Node(a, c, i));
49+
}
50+
51+
int maxLazyTime = Integer.MIN_VALUE;
52+
List<Integer> paths = dijkstra();
53+
for (int path : paths) {
54+
int[] dist = dijkstra(path);
55+
if (dist[n] == 110_000_000) {
56+
maxLazyTime = -1;
57+
break;
58+
} else {
59+
maxLazyTime = Math.max(Math.abs(minDist - dist[n]), maxLazyTime);
60+
}
61+
}
62+
63+
bw.write(Integer.toString(maxLazyTime));
64+
bw.flush();
65+
bw.close();
66+
}
67+
68+
69+
public static List<Integer> dijkstra() {
70+
PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.cost));
71+
int[] dist = new int[n + 1];
72+
Arrays.fill(dist, 110_000_000);
73+
dist[1] = 0;
74+
pq.offer(new Node(1, 0));
75+
76+
while (!pq.isEmpty()) {
77+
Node current = pq.poll();
78+
79+
if (current.number == n) {
80+
minDist = dist[n];
81+
return current.paths;
82+
}
83+
84+
for (Node next : graph[current.number]) {
85+
if (dist[next.number] > dist[current.number] + next.cost) {
86+
dist[next.number] = dist[current.number] + next.cost;
87+
Node select = new Node(next.number, dist[next.number]);
88+
if (current.number != 1) {
89+
select.paths.addAll(current.paths);
90+
}
91+
select.paths.add(next.edge);
92+
pq.offer(select);
93+
}
94+
}
95+
}
96+
97+
98+
return null;
99+
}
100+
101+
public static int[] dijkstra(int edge) {
102+
PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.cost));
103+
int[] dist = new int[n + 1];
104+
Arrays.fill(dist, 110_000_000);
105+
dist[1] = 0;
106+
pq.offer(new Node(1, 0));
107+
108+
while (!pq.isEmpty()) {
109+
Node current = pq.poll();
110+
111+
for (Node next : graph[current.number]) {
112+
if (next.edge == edge) {
113+
continue;
114+
}
115+
if (dist[next.number] > dist[current.number] + next.cost) {
116+
dist[next.number] = dist[current.number] + next.cost;
117+
pq.offer(new Node(next.number, dist[next.number]));
118+
}
119+
}
120+
}
121+
122+
return dist;
123+
}
124+
}

0 commit comments

Comments
 (0)