-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflightDiscout.java
More file actions
143 lines (106 loc) · 3.05 KB
/
Copy pathflightDiscout.java
File metadata and controls
143 lines (106 loc) · 3.05 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.*;
import java.io.*;
public class flightDiscout {
public static ArrayList <trip> [] arr;
public static int [] dist;
public static int m;
public static int n;
public static int [] parents;
public static void main(String[] args) throws IOException{
Scanner s = new Scanner (System.in);
n = s.nextInt();
m = s.nextInt();
dist = new int[n + 1];
for (int i = 1; i <= n; i++) {
dist[i] = Integer.MAX_VALUE;
}
arr = new ArrayList [n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = new ArrayList <trip> ();
}
for (int i = 0; i < m; i++) {
int one = s.nextInt();
int two = s.nextInt();
int weight = s.nextInt();
trip t = new trip(two, weight);
arr[one].add(t);
}
//System.out.println(Arrays.toString(arr));
dijikstra(1);
}
public static void dijikstra (int x) {
boolean [] used = new boolean[n + 1];
parents = new int [n + 1];
dist[x] = 0;
parents[x] = -1;
PriorityQueue <Pair> q = new PriorityQueue <Pair> ();
q.add(new Pair(dist[x], x));
while (!q.isEmpty()) {
int a = q.poll().two;
if (used[a]) {
continue;
}
used[a] = true;
for (trip t : arr[a]) {
int b = t.a;
int w = t.w;
if (dist[a] + w < dist[b]) {
dist[b] = dist[a] + w;
parents[b] = a;
q.add(new Pair(dist[b], b));
}
}
}
//System.out.println(Arrays.toString(parents));
int cur = n;
int prev = n;
ArrayList <Integer> store = new ArrayList <Integer> ();
while (true) {
if (parents[cur] == -1) {
break;
}
prev = cur;
cur = parents[cur];
//System.out.println(cur + " " + prev);
ArrayList <trip> finder = arr[cur];
for (trip t : finder) {
if (t.a == prev) {
store.add(t.w);
}
}
}
Collections.sort(store);
Collections.reverse(store);
//System.out.println(store.get(0));
store.set(0, store.get(0) / 2);
int ans = 0;
for (int i : store) {
ans += i;
}
System.out.println(ans);
}
static class Pair implements Comparable <Pair> {
int one;
int two;
public Pair (int one, int two) {
this.one = one;
this.two = two;
}
@Override
public int compareTo(Pair o) {
return this.one - o.one;
}
}
static class trip {
int a;
int w;
public trip (int x, int z) {
this.a = x;
this.w = z;
}
@Override
public String toString () {
return a + ", " + w + ":";
}
}
}