-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlightRoutes.java
More file actions
94 lines (76 loc) · 2.52 KB
/
FlightRoutes.java
File metadata and controls
94 lines (76 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class FlightRoutes {
static class edge {
int v;
long wt;
edge(int v, long wt) {
this.v = v;
this.wt = wt;
}
}
static class pair implements Comparable<pair> {
int v;
long wsf;
pair(int v, long wsf) {
this.v = v;
this.wsf = wsf;
}
@Override
public int compareTo(pair simpson) {
return (int)(this.wsf - simpson.wsf) ;
}
}
static ArrayList<ArrayList<edge>> al = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
int k = Integer.parseInt(s[2]);
for(int i = 0; i < m; i++) {
al.add(new ArrayList<>());
}
for(int i = 0; i < m; i++) {
String str[] = br.readLine().split(" ");
int u = Integer.parseInt(str[0]);
int v = Integer.parseInt(str[1]);
long c = Long.parseLong(str[2]);
al.get(u).add(new edge(v, c));
}
HashMap<Integer, ArrayList<Long>> map = new HashMap<>();
for(int i = 1; i < n; i++) {
map.put(i, new ArrayList<>());
for(int j = 0; j < k; j++) {
map.get(i).add(Long.MAX_VALUE);
}
}
map.put(0, new ArrayList<>());
for(int i = 0; i < k; i++) {
map.get(0).add((long)0);
}
PriorityQueue<pair> q = new PriorityQueue<>();
q.add(new pair(0, 0));
while (!q.isEmpty()) {
int u = q.peek().v;
long wsf = q.peek().wsf;
q.poll();
if(map.get(u).get(k-1) < wsf) continue;
for(edge e : al.get(u)) {
int v = e.v;
long wt = e.wt;
if(map.get(v).get(k-1) > wt + wsf) {
map.get(v).add(k-1, wt + wsf);
q.add(new pair(v, wt + wsf));
Collections.sort(map.get(v));
}
}
}
for(long i : map.get(n-1)) {
System.out.println(i);
}
System.out.println(Long.MAX_VALUE);
}
}