-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoads.java
76 lines (72 loc) · 1.76 KB
/
Roads.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
package ques;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class Roads {
public static ArrayList<edg>[] grid;
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t>0){
t--;
int k=Integer.parseInt(br.readLine());
int n=Integer.parseInt(br.readLine());
int r=Integer.parseInt(br.readLine());;
String[] s;
grid=new ArrayList[n];
for(int i=0;i<n;i++){
grid[i]=new ArrayList<edg>();
}
for(int i=0;i<r;i++){
s=br.readLine().split(" ");
int sou=Integer.parseInt(s[0])-1;
int des=Integer.parseInt(s[1])-1;
int w=Integer.parseInt(s[2]);
int tax=Integer.parseInt(s[3]);
grid[sou].add(new edg(des,w,tax));
}
int x=dijkstra(k);
if(x==Integer.MAX_VALUE)
System.out.println(-1);
else
System.out.println(x);
}
}
public static int dijkstra(int k){
PriorityQueue<edg> pq=new PriorityQueue<edg>();
pq.add(new edg(0,0,0));
int[] dist=new int[grid.length];
for(int i=0;i<dist.length;i++)
dist[i]=Integer.MAX_VALUE;
while(!pq.isEmpty()){
edg e=pq.poll();
int dest=e.dest;
int len=e.len;
int tax=e.tax;
if(dist[dest]<len)
continue;
dist[dest]=len;
for(int i=0;i<grid[dest].size();i++){
edg f=grid[dest].get(i);
if(f.tax+tax<=k){
pq.add(new edg(f.dest,len+f.len,f.tax+tax));
}
}
}
return dist[dist.length-1];
}
}
class edg implements Comparable<edg>{
int dest,len,tax;
public edg(int dest, int len, int tax){
this.dest=dest;
this.len=len;
this.tax=tax;
}
public int compareTo(edg o) {
if(tax==o.tax)
return len-o.len;
return tax-o.tax;
}
}