forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa01112_MiceandMaze.java
123 lines (102 loc) · 2.51 KB
/
UVa01112_MiceandMaze.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
package uva;
import java.util.*;
import java.io.*;
public class UVa01112_MiceandMaze {
static final long INF = Long.MAX_VALUE / 3;
static class QueueItem implements Comparable<QueueItem> {
int u;
long w;
public QueueItem(int u, long w) {
this.u = u;
this.w = w;
}
public int compareTo(QueueItem q) {
if (this.w < q.w) return -1;
if (this.w > q.w) return 1;
return 0;
}
}
static long[] dijkstra(int s, long[][] adj, int N) {
PriorityQueue<QueueItem> Q = new PriorityQueue<QueueItem>();
long[] d = new long[N];
Arrays.fill(d, INF);
d[s] = 0;
Q.offer(new QueueItem(s, 0));
while (!Q.isEmpty()) {
QueueItem q = Q.poll();
int u = q.u;
long w = q.w;
if (w == d[u]) {
for (int v = 0; v < N; ++v)
if (adj[u][v] >= 0 && d[u] + adj[u][v] < d[v]) {
d[v] = d[u] + adj[u][v];
Q.offer(new QueueItem(v, d[v]));
}
}
}
return d;
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
int K = in.nextInt();
boolean first = true;
while (K-- > 0) {
int N = in.nextInt();
int E = in.nextInt();
long T = in.nextLong();
int M = in.nextInt();
long[][] adj = new long[N][N];
for (int i = 0; i < N; ++i)
Arrays.fill(adj[i], -1);
for (int i = 0; i < M; ++i) {
int from = in.nextInt();
int to = in.nextInt();
long length = in.nextLong();
adj[to - 1][from - 1] = length;
}
long[] d = dijkstra(E - 1, adj, N);
int cnt = 0;
for (int i = 0; i < N; ++i)
if (d[i] <= T)
++cnt;
if (first) first = false; else System.out.println();
System.out.println(cnt);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}