Skip to content

Commit 8555bf1

Browse files
authored
Merge pull request #588 from AlgorithmWithGod/lkhyun
[20250801] BOJ / G3 / 내일로 여행 / 이강현
2 parents b8a3a21 + cc5ad44 commit 8555bf1

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main{
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N, M, R, K;
10+
static Map<String, Integer> cityMap = new HashMap<>();
11+
static List<String> will;
12+
static double[][] normalCost;
13+
static double[][] naeilroCost;
14+
static final int INF = Integer.MAX_VALUE;
15+
16+
public static void main(String[] args) throws Exception {
17+
st = new StringTokenizer(br.readLine());
18+
N = Integer.parseInt(st.nextToken());
19+
R = Integer.parseInt(st.nextToken());
20+
21+
st = new StringTokenizer(br.readLine());
22+
for (int i = 0; i < N; i++) {
23+
cityMap.put(st.nextToken(), i);
24+
}
25+
26+
M = Integer.parseInt(br.readLine());
27+
will = new ArrayList<>();
28+
st = new StringTokenizer(br.readLine());
29+
for (int i = 0; i < M; i++) {
30+
will.add(st.nextToken());
31+
}
32+
33+
normalCost = new double[N][N];
34+
naeilroCost = new double[N][N];
35+
36+
for (int i = 0; i < N; i++) {
37+
for (int j = 0; j < N; j++) {
38+
if (i == j) {
39+
normalCost[i][j] = 0;
40+
naeilroCost[i][j] = 0;
41+
} else {
42+
normalCost[i][j] = INF;
43+
naeilroCost[i][j] = INF;
44+
}
45+
}
46+
}
47+
48+
K = Integer.parseInt(br.readLine());
49+
for (int i = 0; i < K; i++) {
50+
st = new StringTokenizer(br.readLine());
51+
String type = st.nextToken();
52+
String from = st.nextToken();
53+
String to = st.nextToken();
54+
int cost = Integer.parseInt(st.nextToken());
55+
56+
int fromIdx = cityMap.get(from);
57+
int toIdx = cityMap.get(to);
58+
59+
normalCost[fromIdx][toIdx] = Math.min(normalCost[fromIdx][toIdx], cost);
60+
normalCost[toIdx][fromIdx] = Math.min(normalCost[toIdx][fromIdx], cost);
61+
62+
double naeilroPrice = getNaeilroPrice(type, cost);
63+
naeilroCost[fromIdx][toIdx] = Math.min(naeilroCost[fromIdx][toIdx], naeilroPrice);
64+
naeilroCost[toIdx][fromIdx] = Math.min(naeilroCost[toIdx][fromIdx], naeilroPrice);
65+
}
66+
67+
floyd(normalCost);
68+
floyd(naeilroCost);
69+
70+
double totalNormalCost = 0;
71+
double totalNaeilroCost = 0;
72+
73+
for (int i = 0; i < M - 1; i++) {
74+
int from = cityMap.get(will.get(i));
75+
int to = cityMap.get(will.get(i + 1));
76+
77+
totalNormalCost += normalCost[from][to];
78+
totalNaeilroCost += naeilroCost[from][to];
79+
}
80+
81+
totalNaeilroCost += R;
82+
83+
if (totalNaeilroCost < totalNormalCost) {
84+
System.out.println("Yes");
85+
} else {
86+
System.out.println("No");
87+
}
88+
}
89+
90+
static double getNaeilroPrice(String type, double cost) {
91+
if(type.equals("S-Train") || type.equals("V-Train")){
92+
return cost/2;
93+
}else if(type.equals("Mugunghwa") || type.equals("ITX-Cheongchun") || type.equals("ITX-Saemaeul")){
94+
return 0;
95+
}else{
96+
return cost;
97+
}
98+
}
99+
100+
static void floyd(double[][] dist) {
101+
for (int k = 0; k < N; k++) {
102+
for (int i = 0; i < N; i++) {
103+
for (int j = 0; j < N; j++) {
104+
if (dist[i][k] != INF && dist[k][j] != INF) {
105+
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}
112+
```

0 commit comments

Comments
 (0)