-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPOLICE.java
More file actions
103 lines (72 loc) · 1.98 KB
/
Copy pathFPOLICE.java
File metadata and controls
103 lines (72 loc) · 1.98 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
import java.io.*;
import java.util.PriorityQueue;
class FPOLICE {
public static void main(String[]args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int [][] t; //Time associated with each station.
int [][] r; //Risk associated wtih each station.
int cases; //Number of test cases.
String [] bounds; //The array holding N and T.
int n, max_time; //N and T.
cases = Integer.parseInt(br.readLine()); //Number of test cases.
boolean flag = false;
for(int k = 0; k < cases; k++) {
flag = false;
bounds = (br.readLine()).split(" ");
n = Integer.parseInt(bounds[0]);
max_time = Integer.parseInt(bounds[1]);
t = new int [n][n];
r = new int [n][n];
for(int i = 0; i < n; i++) {
String [] row = br.readLine().split(" ");
for(int j = 0; j < n; j++) {
t[i][j] = Integer.parseInt(row[j]);
}
}
for(int i = 0; i < n; i++) {
String [] row = br.readLine().split(" ");
for(int j = 0; j < n; j++) {
r[i][j] = Integer.parseInt(row[j]);
}
}
/* Dijikstra's algorithm */
PriorityQueue <Node> adj = new PriorityQueue <Node> ();
adj.add(new Node(0, 0, 0));
while(!adj.isEmpty()) {
Node c = (Node) adj.remove();
if(c.i == n - 1 && c.time <= max_time) {
System.out.println(c.risk + " " + c.time);
flag = true;
break;
}
for(int j = 0; j < n; j++) {
int new_time = t[c.i][j] + c.time;
if(c.i == j || new_time > max_time)
continue;
adj.add(new Node(j, r[c.i][j] + c.risk, new_time));
}
}
if(!flag) {
System.out.println(-1);
}
}
}
}
class Node implements Comparable <Node> {
int i;
int risk;
int time;
public Node(int i, int risk, int time) {
this.i = i;
this.risk = risk;
this.time = time;
}
public int compareTo(Node n) {
if(risk == n.risk && time != n.time)
return time - n.time;
else if(risk == n.risk && time == n.time)
return i - n.i;
else
return risk - n.risk;
}
}