|
| 1 | +package day1030; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class JY_20007 { |
| 7 | + |
| 8 | + static long INF = Long.MAX_VALUE; |
| 9 | + static int N, M, X, Y; |
| 10 | + static List<Node>[] g; |
| 11 | + static long[] distance; |
| 12 | + static class Node implements Comparable<Node> { |
| 13 | + int n; |
| 14 | + long dist; |
| 15 | + |
| 16 | + public Node(int n, long dist) { |
| 17 | + super(); |
| 18 | + this.n = n; |
| 19 | + this.dist = dist; |
| 20 | + } |
| 21 | + @Override |
| 22 | + public int compareTo(Node other) { |
| 23 | + return Long.compare(this.dist, other.dist); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public String toString() { |
| 28 | + return "Node [n=" + n + ", dist=" + dist + "]"; |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 35 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 36 | + |
| 37 | + N = Integer.parseInt(st.nextToken()); |
| 38 | + M = Integer.parseInt(st.nextToken()); |
| 39 | + X = Integer.parseInt(st.nextToken()); |
| 40 | + Y = Integer.parseInt(st.nextToken()); |
| 41 | + |
| 42 | + g = new ArrayList[N]; |
| 43 | + for(int i=0; i<N; i++) { |
| 44 | + g[i] = new ArrayList<>(); |
| 45 | + } |
| 46 | + |
| 47 | + distance = new long[N]; |
| 48 | + for(int i=0; i<N; i++) { |
| 49 | + distance[i] = INF; |
| 50 | + } |
| 51 | + |
| 52 | + for(int i=0; i<M; i++) { |
| 53 | + st = new StringTokenizer(br.readLine()); |
| 54 | + int a = Integer.parseInt(st.nextToken()); |
| 55 | + int b = Integer.parseInt(st.nextToken()); |
| 56 | + int c = Integer.parseInt(st.nextToken()); |
| 57 | + g[a].add(new Node(b, c)); |
| 58 | + g[b].add(new Node(a, c)); |
| 59 | + } |
| 60 | + |
| 61 | + dijkstra(Y); |
| 62 | + |
| 63 | + Arrays.sort(distance); |
| 64 | + |
| 65 | +// System.out.println(Arrays.toString(distance)); |
| 66 | + |
| 67 | + // 갈 수없는 곳 찾기 |
| 68 | + for(int i=N-1; i>=0; i--) { |
| 69 | + if(2*distance[i] > X) { |
| 70 | + System.out.println(-1); |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // 투포인터 이거 왜안되죠??? |
| 76 | + // X= 21, distance = [0, 1, 1, 9, 9]이면 최소일수는 2일아닌가요??!! ㅠ0ㅠ |
| 77 | +// int s = 0; |
| 78 | +// int e = N-1; |
| 79 | +// long sum = 2*distance[e]; |
| 80 | +// while(s <= e) { |
| 81 | +// if(sum + 2*distance[s] <= X) { |
| 82 | +// sum += 2*distance[s]; |
| 83 | +// s++; |
| 84 | +// } else { |
| 85 | +// e--; |
| 86 | +// ans++; |
| 87 | +// sum = 2*distance[e]; |
| 88 | +// } |
| 89 | +// } |
| 90 | + |
| 91 | + int ans = 1; |
| 92 | + long sum = 0; |
| 93 | + for(int i=0; i<N; i++) { |
| 94 | + if(sum + 2*distance[i] > X) { |
| 95 | + ans++; |
| 96 | + sum = 2*distance[i]; |
| 97 | + } else { |
| 98 | + sum += 2*distance[i]; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + System.out.println(ans); |
| 103 | + |
| 104 | + } |
| 105 | + public static void dijkstra(int start) { |
| 106 | + distance[start] = 0; |
| 107 | + PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 108 | + pq.add(new Node(start, 0)); |
| 109 | + |
| 110 | + while(!pq.isEmpty()) { |
| 111 | + Node now = pq.poll(); |
| 112 | + |
| 113 | + if(distance[now.n] < now.dist) continue; |
| 114 | + |
| 115 | + for(Node next: g[now.n]) { |
| 116 | + long cost = now.dist + next.dist; |
| 117 | + if(distance[next.n] > cost) { |
| 118 | + distance[next.n] = cost; |
| 119 | + pq.add(new Node(next.n, cost)); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments