λ¬Έμ
νμ΄
1οΈβ£ νλ‘μ΄λ μκ³ λ¦¬μ¦κ³Ό ν΄μλ₯Ό μ΄μ©ν νμ΄
π‘ λ μ€λ₯Έ Idea
λ¨Όμ , νλ‘μ΄λ μκ³ λ¦¬μ¦μ μ¬μ©ν΄μ λμμ λμκ° μ΄λ€ κ²½λ‘λ₯Ό κ±°μΉλ μ΅μ λΉμ©μ ꡬν΄μΌκ² λ€κ³ μκ°μ΄ λ€μλ€.
λ€λ§, μ΄ λ¬Έμ μ κ²½μ° λμκ° μ«μκ° μλ λ¬Έμμ΄μ ννλ‘ μ£Όμ΄μ§κΈ° λλ¬Έμ
λμλ₯Ό κ³ μ ν μ«μλ‘ λ³ννμ¬ νλ‘μ΄λ μκ³ λ¦¬μ¦μ μ¬μ©ν΄μΌκ² λ€λ μκ°μ΄ λ€μλ€.
1. λ¨Όμ , μ£Όμ΄μ§λ λμμ μ€λ³΅μ΄ μμ μ μλ€κ³ νμΌλ―λ‘ μ€λ³΅μ μ κ±°νλ€.
μ€λ³΅μ μ κ±°ν λ€, mapμ μ μνμ¬ κ° λμμ μ΄λ¦μ Keyλ‘ νκ³ λ²νΈλ₯Ό 0λΆν° λΆμ¬νλ€.
2. λμμ λμλ‘μ κ΅ν΅μλ¨ μ 보λ₯Ό λ°μ λ, mapμμ λμ μ΄λ¦μΌλ‘ λμ λ²νΈλ₯Ό μ»μ΄μ¬ μ μκ² λλ€.
λ²νΈλ‘ λ³νν λμ 2κ°λ₯Ό μ΄μ©νμ¬ λΉμ©μ 2μ°¨μ λ°°μ΄ κ·Έλνμ μ λ ₯ν΄μ£Όλ©΄λλ€.
λ΄μΌλ‘ ν°μΌμ μ¬μ©ν λΉμ© λ°°μ΄κ³Ό μλ λ°°μ΄ 2κ°λ₯Ό μ μνμ¬ λ°λ‘ μ λ ₯ν΄μ€λ€.
3. λ΄μΌλ‘ ν°μΌμ μ¬μ©νλ€κ³ κ°μ ν λΉμ©κ³Ό μΌλ° λΉμ© 2κ°μ λ°°μ΄μ κ°κ° νλ‘μ΄λ μκ³ λ¦¬μ¦μ ν΅ν΄
κ° λμμμ λμλ‘μ μ΅μ λΉμ© λ°°μ΄μ ꡬνλ€.
4. κ³νν μ¬ν 루νΈλ₯Ό λ°νμΌλ‘ κ° κ²½μ°μ λΉμ© μ΄ν©μ ꡬνκ³ λΉκ΅νμ¬ λ΅μ λ°ννλ€.
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int numOfCities = Integer.parseInt(input[0]);
int R = Integer.parseInt(input[1]);
final int MAX = 10000100;
// λμ μ€λ³΅ μ κ±°
String[] places = getPlaces(br.readLine().split(" "));
// λμ λ³ λ²νΈ λΆμ¬
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < places.length; i++) {
map.put(places[i], i);
}
// μ€λ³΅μ μ κ±°ν λμμ μ
int N = map.size();
int M = Integer.parseInt(br.readLine());
// μ¬νν λμ μμ
String[] trip = br.readLine().split(" ");
int K = Integer.parseInt(br.readLine());
// λ΄μΌλ‘ ν μΈμ΄ μ μ© μλλ λΉμ©
double[][] costs = new double[N][N];
// λ΄μΌλ‘ ν μΈμ΄ μ μ©λλ λΉμ©
double[][] costsWithTicket = new double[N][N];
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
if (r != c) {
costs[r][c] = MAX;
costsWithTicket[r][c] = MAX;
}
}
}
// λμμμ λμλ‘ μ΄λνλ λΉμ© μ
λ ₯
for (int i = 0; i < K; i++) {
input = br.readLine().split(" ");
String type = input[0];
String start = input[1];
String end = input[2];
double cost = Double.parseDouble(input[3]);
// κ΅ν΅ μλ¨μ λ°λ₯Έ ν μΈκ° κ³μ°
double costWithTicket = calculatePrice(type, cost);
int u = map.get(start);
int v = map.get(end);
costs[u][v] = Math.min(costs[u][v], cost);
costs[v][u] = Math.min(costs[v][u], cost);
costsWithTicket[u][v] = Math.min(costsWithTicket[u][v], costWithTicket);
costsWithTicket[v][u] = Math.min(costsWithTicket[v][u], costWithTicket);
}
// νλ‘μ΄λ μκ³ λ¦¬μ¦ μ¬μ©
for (int m = 0; m < N; m++) {
for (int u = 0; u < N; u++) {
for (int v = 0; v < N; v++) {
costs[u][v] = Math.min(costs[u][v], costs[u][m] + costs[m][v]);
costsWithTicket[u][v] = Math.min(costsWithTicket[u][v], costsWithTicket[u][m] + costsWithTicket[m][v]);
}
}
}
// λμμμ λμλ‘ κ°λ μ΅μλΉμ©μ΄ νλ‘μ΄λλ₯Ό ν΅ν΄ κ³μ° λμμΌλ―λ‘ μ΄ ν©μ ꡬνλ€.
double sum = 0;
double sumWithTicket = 0;
for (int i = 0; i < M - 1; i++) {
int u = map.get(trip[i]);
int v = map.get(trip[i + 1]);
sum += costs[u][v];
sumWithTicket += costsWithTicket[u][v];
}
System.out.println(sum <= sumWithTicket + R ? "No" : "Yes");
}
private static String[] getPlaces(String[] arr) {
Set<String> set = new HashSet<>();
for (String city : arr) {
set.add(city);
}
return set.stream().toArray(String[]::new);
}
private static double calculatePrice(String type, double cost) {
Set<String> free = new HashSet<>(List.of("Mugunghwa", "ITX-Saemaeul", "ITX-Cheongchun"));
Set<String> discount = new HashSet<>(List.of("S-Train", "V-Train"));
if (free.contains(type)) {
return 0;
} else if (discount.contains(type)) {
return 0.5 * cost;
} else {
return cost;
}
}
}
κ²°κ³Ό