-
Notifications
You must be signed in to change notification settings - Fork 34
/
A.java
75 lines (69 loc) · 2.49 KB
/
A.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
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* APAC 2016 Round B Problem A: Travel
* Check README.md for explanation.
*/
public class Main {
private String solve(Scanner scanner) {
int n=scanner.nextInt(), m=scanner.nextInt(), k=scanner.nextInt();
int[][][] distance=new int[n][n][24];
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (i!=j)
Arrays.fill(distance[i][j], Integer.MAX_VALUE);
}
}
while (m-->0) {
int x=scanner.nextInt()-1, y=scanner.nextInt()-1;
for (int i=0;i<24;i++) {
distance[x][y][i]=distance[y][x][i]
=Math.min(distance[x][y][i], scanner.nextInt());
}
}
int[][] saved=new int[24][n];
for (int time=0;time<24;time++) {
saved[time]=prepare(n, distance, time);
}
ArrayList<String> arrayList=new ArrayList<>();
while (k-->0) {
int d=scanner.nextInt(), t=scanner.nextInt();
if (d<=n && saved[t][d-1]!=Integer.MAX_VALUE)
arrayList.add(String.valueOf(saved[t][d-1]));
else arrayList.add("-1");
}
return String.join(" ", arrayList);
}
private int[] prepare(int n, int[][][] distance, int time) {
int[] ans=new int[n];
Arrays.fill(ans, Integer.MAX_VALUE);
ans[0]=0;
Queue<Integer> queue=new LinkedList<>();
queue.offer(0);
while (!queue.isEmpty()) {
int curr=queue.poll(), t=(ans[curr]+time)%24;
for (int i=0;i<n;i++) {
if (distance[curr][i][t]!=Integer.MAX_VALUE) {
if (ans[i]>ans[curr]+distance[curr][i][t]) {
ans[i]=ans[curr]+distance[curr][i][t];
queue.offer(i);
}
}
}
}
return ans;
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}