Skip to content

Commit 10d5202

Browse files
authored
Merge pull request #695 from AlgorithmWithGod/lkhyun
[20250819] B형 / SWEA / 경유지운송 / 이강현
2 parents 4ec9374 + dacdc7e commit 10d5202

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
import java.util.*;
3+
class UserSolution {
4+
static class state implements Comparable<state>{
5+
int to,w;
6+
state(int to, int w){
7+
this.to = to;
8+
this.w = w;
9+
}
10+
11+
@Override
12+
public int compareTo(state other){
13+
return Integer.compare(other.w,this.w);
14+
}
15+
}
16+
static List<state>[] adjLists;
17+
18+
public int[] dijkstra(int start){
19+
PriorityQueue<state> pq = new PriorityQueue<>();
20+
int[] dist = new int[1000];
21+
for (int i = 0; i < dist.length; i++) {
22+
dist[i] = -1;
23+
}
24+
for (state cur : adjLists[start]) {
25+
pq.offer(new state(cur.to,cur.w));
26+
dist[cur.to] = cur.w;
27+
}
28+
29+
while(!pq.isEmpty()){
30+
state cur = pq.poll();
31+
32+
if(dist[cur.to] > cur.w) continue;
33+
34+
for (state next : adjLists[cur.to]) {
35+
int newDist = Math.min(cur.w,next.w);
36+
if(newDist > dist[next.to]){
37+
dist[next.to] = newDist;
38+
pq.offer(new state(next.to,newDist));
39+
}
40+
}
41+
}
42+
return dist;
43+
}
44+
45+
public void init(int N, int K, int sCity[], int eCity[], int mLimit[]) {
46+
adjLists = new ArrayList[N];
47+
for (int i = 0; i < N; i++) {
48+
adjLists[i] = new ArrayList<>();
49+
}
50+
for (int i = 0; i < K; i++) {
51+
adjLists[sCity[i]].add(new state(eCity[i],mLimit[i]));
52+
adjLists[eCity[i]].add(new state(sCity[i],mLimit[i]));
53+
}
54+
return;
55+
}
56+
57+
public void add(int sCity, int eCity, int mLimit) {
58+
adjLists[sCity].add(new state(eCity,mLimit));
59+
adjLists[eCity].add(new state(sCity,mLimit));
60+
return;
61+
}
62+
63+
public int calculate(int sCity, int eCity, int M, int mStopover[]) {
64+
int[] fromStart = dijkstra(sCity);
65+
int ans = fromStart[eCity];
66+
for (int i = 0; i < M; i++) {
67+
if(fromStart[mStopover[i]] == -1){
68+
return -1;
69+
}
70+
ans = Math.min(ans, fromStart[mStopover[i]]);
71+
}
72+
return ans;
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)