-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlmostShortestPath.java
140 lines (119 loc) · 3.42 KB
/
AlmostShortestPath.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
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
/**
* https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3296
* tag: #dijkstra #shortest-path use dijkstra k times
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
class AlmostShortestPath {
static int max_val = (int) 1e8;
static int Dijkstra(ArrayList<Node>[] graph, int source, int destination, boolean visitedPath[]) {
int len = graph.length;
int[] distArr = new int[len];
int[] pathArr = new int[len];
Arrays.fill(distArr, max_val);
Arrays.fill(pathArr, -1);
distArr[source] = 0;
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(source, distArr[source]));
while (!pq.isEmpty()) {
Node node = pq.poll();
int id = node.id;
int w = node.dist;
if (w > distArr[id] || visitedPath[id]) {
continue;
}
for (Node neighbour : graph[id]) {
if (!visitedPath[neighbour.id] && (w + neighbour.dist < distArr[neighbour.id])) {
distArr[neighbour.id] = w + neighbour.dist;
pathArr[neighbour.id] = id;
pq.add(new Node(neighbour.id, distArr[neighbour.id]));
}
}
}
int st = pathArr[destination];
while (st != -1 && st != source) {
visitedPath[st] = true;
st = pathArr[st];
}
return distArr[destination] == max_val ? (-1) : distArr[destination];
}
public static void main(String[] args) {
MyScanner sc = new MyScanner(System.in);
while (true) {
int n = sc.nextInt();
int m = sc.nextInt();
if (n == 0 && m == 0) break;
int source = sc.nextInt();
int destination = sc.nextInt();
ArrayList<Node>[] graph = new ArrayList[n];
for (int i = 0; i < graph.length; i++) {
graph[i] = new ArrayList<Node>();
}
for (int i = 0; i < m; ++i) {
int u = sc.nextInt();
int v = sc.nextInt();
int p = sc.nextInt();
graph[u].add(new Node(v, p));
}
boolean[] visitedPath = new boolean[n];
int minimumCost = Dijkstra(graph, source, destination, visitedPath);
int almostMinimumCost = minimumCost;
while (minimumCost != -1 && almostMinimumCost == minimumCost) {
almostMinimumCost = Dijkstra(graph, source, destination, visitedPath);
}
System.out.println(almostMinimumCost);
}
}
}
class Node implements Comparable<Node> {
int id;
int dist;
Node(int id, int dist) {
this.id = id;
this.dist = dist;
}
public int compareTo(Node other) {
return this.dist - other.dist;
}
}
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}