-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
91 lines (77 loc) · 2.64 KB
/
Main.java
File metadata and controls
91 lines (77 loc) · 2.64 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int[] dist;
static ArrayList<Edge>[] dependency;
static PriorityQueue<Edge> q;
public static void main(String[] arg) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int tc = Integer.parseInt(st.nextToken());
for(int i=0; i<tc; i++){
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken()); // 컴퓨터 대수
int d = Integer.parseInt(st.nextToken()); // 의존성 개수
int c = Integer.parseInt(st.nextToken()); // 해킹당한 컴퓨터 번호
dist = new int[n+1];
Arrays.fill(dist, Integer.MAX_VALUE);
dependency = new ArrayList[n+1];
for(int k=1; k<n+1; k++){
dependency[k] = new ArrayList<Edge>();
}
for(int j=0; j<d; j++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int s = Integer.parseInt(st.nextToken());
dependency[b].add(new Edge(a,s)); // b -> a 해킹
}
dijkstra(c);
int cnt = 0;
int maxDistance = 0;
for(int sd : dist){
if( sd != Integer.MAX_VALUE){
cnt++;
if(sd > maxDistance){
maxDistance = sd;
}
}
}
System.out.println(cnt+ " " + maxDistance);
}
br.close();
}
public static void dijkstra(int c){
q = new PriorityQueue<Edge>((Edge e1, Edge e2)->{
return Integer.compare(e1.dist, e2.dist);
});
q.add(new Edge(c, 0));
dist[c] = 0;
while(!q.isEmpty()){
Edge curr = q.poll();
if(dist[curr.to] < curr.dist){
continue;
}
for(Edge adj : dependency[curr.to]){
int cost = adj.dist + dist[curr.to];
if(dist[adj.to] > cost){
dist[adj.to] = cost;
q.add(new Edge(adj.to, cost));
}
}
}
}
}
class Edge {
int to;
int dist;
public Edge(int to, int dist){
this.to = to;
this.dist = dist;
}
}