-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ10282_1.java
More file actions
87 lines (70 loc) · 2.19 KB
/
Copy pathJ10282_1.java
File metadata and controls
87 lines (70 loc) · 2.19 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
package TIL;
import java.util.*;
public class J10282_1 {
static int n, d, c;
static ArrayList<Node>[] list;
static int count;
static boolean[] visited;
static int[] dist;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for(int i = 0; i < t; i++) {
n = scan.nextInt();
d = scan.nextInt();
c = scan.nextInt();
list = new ArrayList[n + 1];
for(int j = 1; j <= n; j++) {
list[j] = new ArrayList<>();
}
for(int j = 0; j < d; j++) {
int a = scan.nextInt();
int b = scan.nextInt();
int s = scan.nextInt();
list[b].add(new Node(a, s));
}
count = 0;
dist = new int[n + 1];
visited = new boolean[n + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[c] = 0;
dijkstra();
int time = 0;
for(int j = 1; j <= n; j++) {
if(dist[j] != Integer.MAX_VALUE) time = Math.max(time, dist[j]);
}
System.out.println(count + " " + time);
}
}
public static void dijkstra() {
PriorityQueue<Node> q = new PriorityQueue<>();
q.offer(new Node(c, 0));
while(!q.isEmpty()) {
Node current = q.poll();
if(visited[current.n] == false) {
visited[current.n] = true;
count++;
}
else continue;
for(int i = 0; i < list[current.n].size(); i++) {
Node next = list[current.n].get(i);
if(dist[next.n] > dist[current.n] + next.s) {
dist[next.n] = dist[current.n] + next.s;
q.offer(new Node(next.n, dist[next.n]));
}
}
}
}
public static class Node implements Comparable<Node> {
int n;
int s;
public Node(int n, int s) {
this.n = n;
this.s = s;
}
@Override
public int compareTo(Node n) {
return this.s - n.s;
}
}
}