β¨ μ΅λ¨κ²½λ‘ μκ³ λ¦¬μ¦ β‘οΈ λ²¨λ§ ν¬λ μκ³ λ¦¬μ¦
https://www.acmicpc.net/problem/1865
1865λ²: μν
첫 λ²μ§Έ μ€μλ ν μ€νΈμΌμ΄μ€μ κ°μ TC(1 ≤ TC ≤ 5)κ° μ£Όμ΄μ§λ€. κ·Έλ¦¬κ³ λ λ²μ§Έ μ€λΆν° TCκ°μ ν μ€νΈμΌμ΄μ€κ° μ°¨λ‘λ‘ μ£Όμ΄μ§λλ° κ° ν μ€νΈμΌμ΄μ€μ 첫 λ²μ§Έ μ€μλ μ§μ μ μ N(1 ≤ N ≤ 500),
www.acmicpc.net
π κ³ λ €ν΄μΌν μ
- ν μ§μ μμ μΆλ°νμ¬ λ€μ μΆλ°νλ μ§μ μΌλ‘ λμμμ λ μ²μ μΆλ°νμ λλ³΄λ€ μκ°μ΄ λλμκ° μλ κ²½μ° νλ¨ β μν λ΄μμ μκ°μ΄ κ±°κΎΈλ‘ νλ¬κ°λ€. β‘οΈ μμ κ°μ€μΉ μ‘΄μ¬ & μμ μ¬μ΄ν΄ μ‘΄μ¬ O
- ν μ§μ μμ μΆλ°νλ€. (νΉμ μ§μ μμ μΆλ° X)
- μμμ μ μ μμ μΆλ°
- λͺ¨λ μ μ μμ μΆλ°
- μμμ μ μ μμ μΆλ°
- λ μ§μ μ μ°κ²°νλ λλ‘κ° ν κ°λ³΄λ€ λ§μ μ μλ€. β μΈμ 리μ€νΈλ‘ ꡬν
- λ μ§μ μ¬μ΄μ λλ‘λ μλ°©ν₯μ΄κ³ , μνμ λ¨λ°©ν₯μ΄λ€.
- μμ μ¬μ΄ν΄ μμΌλ©΄ YES
- μμ μ¬μ΄ν΄ μμΌλ©΄ NO
1οΈβ£ μμμ μ μ μμ μΆλ°
if (dist[info.end] > dist[j] + info.weight) {
dist[info.end] = dist[j] + info.weight;
isUpdate = true;
}
dist[j] != INF
μ‘°κ±΄μ΄ μμΌλ©΄ μΆλ°μ μμ λ€λ₯Έ μ μ μΌλ‘μ κ°μ μ΄ μμ΄μ(λ¨μ λ κ³³) INFμΈ κ²½μ°, λ€λ₯Έ μ μ λ€μμ μμ μ¬μ΄ν΄μ΄ λ§λ€μ΄μ Έλ λμ΄μ νμνμ§ λͺ»νκ³ NOλ₯Ό μΆλ ₯νκ² λλ€. κ·Έλ¬λ―λ‘ μ‘°κ±΄μ μ κ±°νκ³ μμ κ°μ΄ μ½λλ₯Ό μμ±ν΄μΌ νλ€.
2οΈβ£ λͺ¨λ μ μ μμ μΆλ°
if (dist[j] != INF && dist[info.end] > dist[j] + info.weight) {
dist[info.end] = dist[j] + info.weight;
isUpdate = true;
}
νΉμ μ μ μμ κ°μ μ΄ μμ΄μ νμνμ§ λͺ»ν΄λ λ€λ₯Έ μ μ λ€μμ νμ κ°λ₯νλ―λ‘ μ‘°κ±΄μ λ£μ΄μ μ½λλ₯Ό μμ±νλ€.
πΉοΈ νμ΄κ³Όμ
1οΈβ£ κ°μ κ³Ό μ μ μ μ 보λ₯Ό μ μ₯νλ€.
2οΈβ£ μ μ μλ§νΌ 벨λ§ν¬λ μκ³ λ¦¬μ¦μ μννλ€.
π©π» μ 체 μ½λ
public class BOJ_1865_μν {
static int n, m, w; // n=μ§μ μ μ, m=λλ‘μ μ, w=μνμ μ
static ArrayList<Info>[] graph;
static int INF = 1000000000;
static int[] dist;
static class Info{
int e, t;
public Info(int e, int t) {
this.e = e;
this.t = t;
}
}
static boolean bellmanFord(){
dist[1] =0;
for (int i = 1; i < n; i++) {
for (int j = 1; j < graph.length; j++) {
for (Info next : graph[j]) {
if (dist[next.e] > dist[j] + next.t) {
dist[next.e] = dist[j] + next.t;
}
}
}
}
for (int j = 1; j < graph.length; j++) {
for (Info next : graph[j]) {
if (dist[next.e] > dist[j] + next.t) {
return true;
}
}
}
return false;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int TC = Integer.parseInt(br.readLine());
for (int i = 0; i < TC; i++) {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
dist = new int[n + 1];
Arrays.fill(dist, INF);
graph = new ArrayList[n + 1];
for (int j = 0; j < n + 1; j++) {
graph[j] = new ArrayList<>();
}
// λλ‘μ μ 보
// Sμ Eλ μ°κ²°λ μ§μ μ λ²νΈ, Tλ μ΄ λλ‘λ₯Ό ν΅ν΄ μ΄λνλλ° κ±Έλ¦¬λ μκ°
for (int j = 0; j < m; j++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
graph[s].add(new Info(e, t));
graph[e].add(new Info(s, t));
}
// μνμ μ 보
for (int j = 0; j < w; j++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken()); // μμ μ§μ
int e = Integer.parseInt(st.nextToken()); // λμ°© μ§μ
int t = Integer.parseInt(st.nextToken()); // μ€μ΄λλ μκ°
graph[s].add(new Info(e, -t));
}
if (bellmanFord()) System.out.println("YES");
else System.out.println("NO");
}
}
}
μ°Έκ³
https://steady-coding.tistory.com/91
https://sorjfkrh5078.tistory.com/30
'μκ³ λ¦¬μ¦ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 4179 λΆ! - Java (1) | 2023.10.25 |
---|---|
[BOJ] 14502 μ°κ΅¬μ - Java (0) | 2023.09.28 |
[BOJ] 3190 λ± (0) | 2023.09.25 |
[BOJ] 11657 νμλ¨Έμ (1) | 2023.09.08 |
[BOJ] 1939 μ€λμ ν (0) | 2023.09.07 |