Skip to content

Commit b80c939

Browse files
authored
Create Code1.java
1 parent 948b96b commit b80c939

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

Juspay Round-1/Code1.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import java.util.ArrayList;
2+
import java.util.PriorityQueue;
3+
import java.util.Scanner;
4+
import java.util.Arrays;
5+
6+
/* IMPORTANT: Multiple classes and nested static classes are supported */
7+
8+
/*
9+
* uncomment this if you want to read input.
10+
//imports for BufferedReader
11+
import java.io.BufferedReader;
12+
import java.io.InputStreamReader;
13+
14+
//import for Scanner and other utility classes
15+
import java.util.*;
16+
*/
17+
18+
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
19+
20+
class TestClass {
21+
public static void main(String args[] ) throws Exception {
22+
/* Sample code to perform I/O:
23+
* Use either of these methods for input
24+
25+
//BufferedReader
26+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
27+
String name = br.readLine(); // Reading input from STDIN
28+
System.out.println("Hi, " + name + "."); // Writing output to STDOUT
29+
30+
//Scanner
31+
Scanner s = new Scanner(System.in);
32+
String name = s.nextLine(); // Reading input from STDIN
33+
System.out.println("Hi, " + name + "."); // Writing output to STDOUT
34+
35+
*/
36+
37+
// Write your code here
38+
Scanner sc = new Scanner(System.in);
39+
int n =sc.nextInt();
40+
int[] node =new int[n];
41+
int maxi=0;
42+
for(int i=0;i<n;i++){
43+
node[i] =sc.nextInt();
44+
maxi =Math.max(node[i],maxi);
45+
}
46+
int edge=sc.nextInt();
47+
ArrayList<ArrayList<Pair>> adj =new ArrayList<>();
48+
for(int i=0;i<maxi+1;i++){
49+
adj.add(new ArrayList<>());
50+
}
51+
for(int i=0;i<edge;i++){
52+
int u =sc.nextInt();
53+
int v =sc.nextInt();
54+
int dist =sc.nextInt();
55+
adj.get(u).add(new Pair(dist,v));
56+
adj.get(v).add(new Pair(dist,u));
57+
}
58+
int src =sc.nextInt();
59+
int dest =sc.nextInt();
60+
61+
int shortesttime =sourceDestination(src, dest, adj);
62+
System.out.print(shortesttime);
63+
}
64+
public static int sourceDestination(int src ,int dest,ArrayList<ArrayList<Pair>> adj){
65+
66+
int[] dst = new int[1000001];
67+
Arrays.fill(dst,(int)1e6);
68+
PriorityQueue<Pair> pq =new PriorityQueue<>((a,b)->((a.dist==b.dist)?(a.node-b.node):(a.dist-b.dist)));
69+
70+
pq.offer(new Pair(0,src));
71+
dst[src]=0;
72+
while(!pq.isEmpty()){
73+
Pair curr =pq.poll();
74+
int currnode =curr.node;
75+
int dis =curr.dist;
76+
77+
if(currnode == dest){
78+
return dis;
79+
}
80+
81+
for(Pair temp :adj.get(currnode)){
82+
int tempnode = temp.node;
83+
int d =temp.dist;
84+
if(dis+d<dst[tempnode]){
85+
dst[tempnode]=dis+d;
86+
pq.offer(new Pair(dst[tempnode],tempnode));
87+
}
88+
}
89+
}
90+
return -1;
91+
92+
}
93+
static class Pair{
94+
int dist;
95+
int node;
96+
Pair(int _dist,int _node){
97+
this.dist=_dist;
98+
this.node=_node;
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)