Skip to content

Commit b72693e

Browse files
authored
Merge pull request #478 from AlgorithmWithGod/khj20006
[20250716] BOJ / P5 / 오민식의 고민 / 권혁준
2 parents 39a5930 + 89f9d79 commit b72693e

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static final long INF = -(long)1e18 + 7;
57+
58+
static int N, M, A, B;
59+
static int[] earn;
60+
static List<int[]> edges;
61+
62+
public static void main(String[] args) throws Exception {
63+
64+
io = new IOController();
65+
66+
init();
67+
solve();
68+
69+
io.close();
70+
71+
}
72+
73+
public static void init() throws Exception {
74+
75+
N = io.nextInt();
76+
A = io.nextInt();
77+
B = io.nextInt();
78+
M = io.nextInt();
79+
edges = new ArrayList<>();
80+
for(int i=0;i<M;i++){
81+
int a = io.nextInt();
82+
int b = io.nextInt();
83+
int c = io.nextInt();
84+
edges.add(new int[] {a,b,c});
85+
}
86+
earn = new int[N];
87+
for(int i=0;i<N;i++) earn[i] = io.nextInt();
88+
89+
}
90+
91+
static void solve() throws Exception {
92+
93+
boolean[] fromA = new boolean[N];
94+
boolean[] toB = new boolean[N];
95+
Queue<Integer> q = new ArrayDeque<>();
96+
boolean[] vis = new boolean[N];
97+
q.add(A);
98+
vis[A] = true;
99+
while(!q.isEmpty()){
100+
int cur = q.poll();
101+
fromA[cur] = true;
102+
for(int[] edge: edges) if(edge[0] == cur) {
103+
if(!vis[edge[1]]) {
104+
q.add(edge[1]);
105+
vis[edge[1]] = true;
106+
}
107+
}
108+
}
109+
110+
for(int i=0;i<N;i++) {
111+
vis = new boolean[N];
112+
q.add(i);
113+
vis[i] = true;
114+
while(!q.isEmpty()){
115+
int cur = q.poll();
116+
if(cur == B) toB[i] = true;
117+
for(int[] edge:edges) if(edge[0] == cur) {
118+
if(!vis[edge[1]]) {
119+
q.add(edge[1]);
120+
vis[edge[1]] = true;
121+
}
122+
}
123+
}
124+
}
125+
126+
long[] dist = new long[N];
127+
Arrays.fill(dist, INF);
128+
dist[A] = -earn[A];
129+
for(int step=1;step<=N;step++) {
130+
long before = dist[A];
131+
for(int[] edge:edges) {
132+
int from = edge[0], to = edge[1], cost = edge[2];
133+
if(dist[from] != INF && (dist[to] == INF || dist[to] > dist[from] + cost - earn[to])) {
134+
dist[to] = dist[from] + cost - earn[to];
135+
if(step == N && fromA[to] && toB[to]) {
136+
io.write("Gee");
137+
return;
138+
}
139+
}
140+
}
141+
}
142+
143+
if(dist[B] == INF) io.write("gg");
144+
else io.write(-dist[B] + "\n");
145+
146+
}
147+
148+
}
149+
```

0 commit comments

Comments
 (0)