-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
118 lines (103 loc) · 3.5 KB
/
Main.java
File metadata and controls
118 lines (103 loc) · 3.5 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static ArrayList<Node>[] adj;
static ArrayList<Node>[] reverseAdj;
static boolean[][] dropped;
static int[] distance;
static int start, end;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
while(true){
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
if(n == 0 && m == 0) break;
st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
end = Integer.parseInt(st.nextToken());
adj = new ArrayList[n+1];
for(int i=0; i<n+1; i++){
adj[i] = new ArrayList<Node>();
}
reverseAdj = new ArrayList[n+1];
for(int i=0; i<n+1; i++){
reverseAdj[i] = new ArrayList<Node>();
}
for(int i=0;i <m; i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
adj[x].add(new Node(y, cost));
reverseAdj[y].add(new Node(x, cost));
}
dropped = new boolean[n+1][n+1];
for(int i=0; i<n+1; i++){
Arrays.fill(dropped[i], Boolean.FALSE);
}
distance = new int[n+1];
Arrays.fill(distance, Integer.MAX_VALUE);
dijkstra();
bfs();
Arrays.fill(distance, Integer.MAX_VALUE);
dijkstra();
if(distance[end] != Integer.MAX_VALUE){
System.out.println(distance[end]);
} else {
System.out.println(-1);
}
}
br.close();
}
public static void dijkstra(){
PriorityQueue<Node> pq = new PriorityQueue<Node>((Node n1, Node n2)->{
return Integer.compare(n1.dist, n2.dist);
});
pq.add(new Node(start, 0));
distance[start]=0;
while(!pq.isEmpty()){
Node curr = pq.poll();
if(distance[curr.idx] < curr.dist) continue;
for(Node adj : adj[curr.idx]){
int cost = curr.dist + adj.dist;
if(distance[adj.idx] > cost && !dropped[curr.idx][adj.idx]){
distance[adj.idx] = cost;
pq.add(new Node(adj.idx, cost));
}
}
}
}
public static void bfs(){
Queue<Integer> q = new LinkedList<Integer>();
q.add(end);
while(!q.isEmpty()){
int curr = q.poll();
if (curr == start){
continue;
}
for(Node adj : reverseAdj[curr]){
if(distance[curr] == (distance[adj.idx] + adj.dist) ){
dropped[adj.idx][curr] = true;
q.add(adj.idx);
}
}
}
}
}
class Node {
int idx;
int dist;
public Node(int idx, int dist){
this.idx = idx;
this.dist = dist;
}
}