-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlightDiscount.java
78 lines (75 loc) · 2.5 KB
/
FlightDiscount.java
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
//package com.company.CSES;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class FlightDiscount {
public static class edge {
int v;
long wt;
edge(int v, long wt) {
this.v = v;
this.wt = wt;
}
}
public static class pair implements Comparable<pair> {
int val;
long wsf;
pair(int val, long wsf) {
this.val = val;
this.wsf = wsf;
}
@Override
public int compareTo(pair simpson) {
return (int)(this.wsf - simpson.wsf);
}
}
static ArrayList<ArrayList<edge>> a = new ArrayList<>();
static ArrayList<ArrayList<edge>> b = new ArrayList<>();
static boolean[] visited;
public static void dij(long[] dis, int src, ArrayList<ArrayList<edge>> a) {
visited = new boolean[100005];
PriorityQueue<pair> q = new PriorityQueue<>();
q.add(new pair(src, 0));
while(!q.isEmpty()) {
pair rem = q.remove();
if(visited[rem.val]) continue;
visited[rem.val] = true;
dis[rem.val] = rem.wsf;
for(edge e : a.get(rem.val)) {
q.add(new pair(e.v, rem.wsf + e.wt));
}
}
}
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]);
for(int i = 0; i <= n; i++) {
a.add(new ArrayList<>());
b.add(new ArrayList<>());
}
for(int i = 0; i < m; i++) {
String s1[] = br.readLine().split(" ");
int u = Integer.parseInt(s1[0]);
int v = Integer.parseInt(s1[1]);
int wt = Integer.parseInt(s1[2]);
a.get(u).add(new edge(v, wt));
b.get(v).add(new edge(u, wt));
}
long[] dis1 = new long[100005];
long[] dis2 = new long[100005];
dij(dis1, 1, a);
dij(dis2, n, b);
long ans = Long.MAX_VALUE;
for(int i = 1; i <= n; i++) {
ArrayList<edge> e = a.get(i);
for(edge e1 : e)
ans = Math.min(ans, dis1[i] + dis2[e1.v] + e1.wt/2);
}
System.out.println(ans);
}
}