알고리즘 문제/BOJ
10217번 KCM Travel
parkit
2020. 4. 16. 23:19
728x90
반응형
https://www.acmicpc.net/problem/10217
java vector 우선순위 큐 Pair pair class 자바
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import java.io.*; import java.util.*; public class Main { static int n, m, k; static int[][] dist; static PriorityQueue<Pair> pq; static Vector<Vector<info>> V; static BufferedReader br; static BufferedWriter bw; static int dijstra() throws IOException { pq = new PriorityQueue<Pair>(); // 현재 총 소모 시간, 현재 위치, 현재까지 사용한 돈(비용) pq.add(new Pair(0, 1, 0)); dist = new int[n + 1][10001]; // [현재 위치][현재까지 사용한 돈(비용)] for(int i=1; i<=n; i++) { Arrays.fill(dist[i], 100000000); } dist[1][0] = 0; while(!pq.isEmpty()) { int total_time = pq.peek().first; int here = pq.peek().second; int used_money = pq.peek().third; pq.remove(); if(dist[here][used_money] < total_time) { continue; } for(info next : V.get(here)) { int sum = used_money + next.money; if(sum <= m && dist[next.end][sum] > dist[here][used_money] + next.time) { dist[next.end][sum] = dist[here][used_money] + next.time; pq.add(new Pair(dist[next.end][sum], next.end, sum)); } } } int ret = 100000000; for(int i=1; i<=m; i++) { if(dist[n][i] != 100000000) { ret = Math.min(ret, dist[n][i]); } } return ret; } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; int t; t = Integer.parseInt(br.readLine()); for(int tc=0; tc<t; tc++) { st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); k = Integer.parseInt(st.nextToken()); int u, v, c, d; V = new Vector<Vector<info>>(); for(int i=0; i<n+1; i++) { Vector<info> vt = new Vector<info>(); V.add(vt); } for(int i=0; i<k; i++) { st = new StringTokenizer(br.readLine()); u = Integer.parseInt(st.nextToken()); v = Integer.parseInt(st.nextToken()); c = Integer.parseInt(st.nextToken()); d = Integer.parseInt(st.nextToken()); V.get(u).add(new info(v, c, d)); } int g = dijstra(); if(g==100000000) { bw.write(String.valueOf("Poor KCM") + "\n"); } else { bw.write(String.valueOf(g) + "\n"); } } bw.flush(); bw.close(); } } class Pair implements Comparable<Pair> { int first, second, third; Pair(int f, int s, int t) { this.first = f; this.second = s; this.third = t; } public int compareTo(Pair p) { if(this.first < p.first) { return -1; // 오름차순 } else if(this.first == p.first) { if(this.second < p.second) { return -1; } else if(this.second == p.second) { if(this.third < p.third) { return -1; } } } return 1; // 이미 this.first가 더 큰 것이 됐으므로, 1로 해준다. // -1로 하면 결과가 이상하게 출력됨. } } class info { int end, money, time; info(int a, int b, int c) { this.end = a; this.money = b; this.time = c; } } | cs |
728x90
반응형