forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa00341_NonStopTravel.java
112 lines (96 loc) · 2.35 KB
/
UVa00341_NonStopTravel.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
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 277 (341 - Non-Stop Travel) */
/* SUBMISSION: 10359882 */
/* SUBMISSION TIME: 2012-07-19 22:58:33 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00341_NonStopTravel {
static Vertex[] G;
static int[] p;
static int N;
static final int INF = Integer.MAX_VALUE;
static class Vertex {
List<Edge> adj;
public Vertex() {
this.adj = new ArrayList<Edge>();
}
}
static class Edge {
int to, w;
public Edge(int to, int w) {
this.to = to; this.w = w;
}
}
static class QueueItem implements Comparable<QueueItem> {
int u, dist;
public QueueItem(int u, int dist) {
this.u = u; this.dist = dist;
}
public int compareTo(QueueItem q) {
return this.dist - q.dist;
}
}
static int dijkstra(int s, int e) {
int[] dist = new int[N];
p = new int[N];
PriorityQueue<QueueItem> Q = new PriorityQueue<QueueItem>();
Arrays.fill(dist, INF);
Arrays.fill(p, -1);
dist[s] = 0;
Q.offer(new QueueItem(s, 0));
while (!Q.isEmpty()) {
QueueItem q = Q.poll();
int u = q.u;
if (dist[u] < INF) {
if (u == e)
return dist[u];
for (Edge ed : G[u].adj) {
int v = ed.to, w = ed.w;
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
p[v] = u;
Q.offer(new QueueItem(v, dist[v]));
}
}
}
}
return -1;
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int t = 1;
while (true) {
N = in.nextInt();
if (N == 0) break;
G = new Vertex[N];
for (int i = 0; i < N; ++i)
G[i] = new Vertex();
for (int from = 0; from < N; ++from) {
int M = in.nextInt();
for (int j = 0; j < M; ++j) {
int to = in.nextInt() - 1;
int w = in.nextInt();
G[from].adj.add(new Edge(to, w));
}
}
int s = in.nextInt() - 1;
int e = in.nextInt() - 1;
int dist = dijkstra(s, e);
Stack<Integer> path = new Stack<Integer>();
int cur = e;
while (cur != -1) {
path.add(cur);
cur = p[cur];
}
System.out.print("Case " + t + ": Path =");
while (!path.isEmpty())
System.out.print(" " + (path.pop() + 1));
System.out.println("; " + dist + " second delay");
++t;
}
in.close();
System.exit(0);
}
}