-
Notifications
You must be signed in to change notification settings - Fork 0
/
besslamotors.java
67 lines (56 loc) · 1.82 KB
/
besslamotors.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
// O (KM log N)
import java.util.*;
import java.io.*;
public class besslamotors {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()), M = Integer.parseInt(st.nextToken()), C = Integer.parseInt(st.nextToken());
int R = Integer.parseInt(st.nextToken()), K = Integer.parseInt(st.nextToken());
ArrayList<Edge>[] adj = new ArrayList[N];
HashSet<Integer>[] mark = new HashSet[N];
for(int i = 0; i < N; i++) {
adj[i] = new ArrayList<>();
mark[i] = new HashSet<>();
}
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken())-1, v = Integer.parseInt(st.nextToken())-1, l = Integer.parseInt(st.nextToken());
adj[u].add(new Edge(v, l, v));
adj[v].add(new Edge(u, l, u));
}
br.close();
int[] cnct = new int[N];
PriorityQueue<Edge> toVisit = new PriorityQueue<>();
for(int i = 0; i < C; i++) toVisit.add(new Edge(i, 0, i));
while(!toVisit.isEmpty()) {
int curr = toVisit.peek().to, src = toVisit.peek().src;
long w = toVisit.peek().l;
toVisit.remove();
if(w > R || mark[curr].contains(src) || cnct[curr] >= K) continue;
mark[curr].add(src);
cnct[curr]++;
for(Edge e : adj[curr]) {
toVisit.add(new Edge(e.to, w + e.l, src));
}
}
int cnt = 0;
for(int i = C; i < N; i++)
if(cnct[i] >= K) cnt++;
System.out.println(cnt);
for(int i = C; i < N; i++)
if(cnct[i] >= K) System.out.println(i+1);
}
static class Edge implements Comparable<Edge>{
int to, src;
long l;
Edge(int to, long l, int src) {
this.to = to;
this.l = l;
this.src = src;
}
public int compareTo(Edge o) {
return Long.compare(l, o.l);
}
}
}