[BOJ] 1865 μν
https://www.acmicpc.net/problem/1865
λ¬Έμ μ κ·Ό
λ²¨λ§ ν¬λ μκ³ λ¦¬μ¦μ λ€μκ³Ό κ°λ€.
1.μμ λ
Έλλ₯Ό μ€μ νλ€.
2.μ΅λ¨ κ²½λ‘ ν
μ΄λΈ(λ°°μ΄)μ μ΄κΈ°ννλ€.
3.(μ μ μ κ°μ-1)λ²λμ λͺ¨λ κ°μ μ νμνμ¬ μ΅λ¨ κ²½λ‘ ν
μ΄λΈμ κ°±μ νλ€.
μ¬κΈ°μ μμ μνμ΄ λ°μνλμ§ μκ³ μΆλ€λ©΄ νλ² λ λͺ¨λ κ°μ μ λν΄μ νμμ νκ³ μ΄λ, μ΅λ¨ κ²½λ‘ ν
μ΄λΈμ΄ κ°±μ μ΄ λλ€λ©΄ μμ μνμ΄ λ°μν κ²μ΄λ€.
μ΄ λ¬Έμ λ λͺ¨λ λ
Έλλ₯Ό μμ λ
Έλλ₯Ό μ€μ ν νμκ° μμ΄ μμ μνμ΄ μλμ§ μλμ§λ§ 체ν¬νλ©΄ λκΈ°λλ¬Έμ 1λ² λ
Έλμ λν΄μλ§ λ²¨λ§ ν¬λ μκ³ λ¦¬μ¦μ μννκ³ μ΄λ, μμ μνμ΄ λ°μνλμ§ μλμ§λ₯Ό κ²μ¬νμλ€.
Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class BOJ1865 {
static ArrayList<ArrayList<Node>> list;
static int[] dist;
static String answer;
static int n;
static private final int INF = 987654321;
static class Node {
int to;
int time;
Node(int to, int time){
this.to = to;
this.time = time;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
while(T-- > 0){
StringTokenizer st = new StringTokenizer(br.readLine()," ");
n = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
list = new ArrayList<>();
for(int i =0 ;i<=n; i++){
list.add(new ArrayList<>());
}
//== μ
λ ₯ λ°κΈ° ==//
for(int i = 1; i<=v+w; i++){
st = new StringTokenizer(br.readLine()," ");
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
if(i <= v){
list.get(s).add(new Node(e,t));
list.get(e).add(new Node(s,t));
} else {
list.get(s).add(new Node(e,t*-1));
}
}
//== λ²¨λ§ ν¬λ μκ³ λ¦¬μ¦ μν==//
dist = new int[n+1];
answer = bellman_ford() ? "YES":"NO";
sb.append(answer+"\n");
}
System.out.print(sb.toString());
}
//==벨λ§-ν¬λ μκ³ λ¦¬μ¦==//
public static boolean bellman_ford(){
//λ°°μ΄ μ΄κΈ°ν
Arrays.fill(dist,INF);
//μμ μ§μ μ΄κΈ°ν
dist[1] = 0;
boolean update = false;
// n-1λ²κΉμ§
for(int i =1; i< n; i++){
update = false;
//== μ΅λ¨κ±°λ¦¬ μ΄κΈ°ν==//
for(int j =1; j<= n; j++){
for(Node n: list.get(j)){
if(dist[n.to]>dist[j] + n.time){
dist[n.to] = dist[j] + n.time;
update = true;
}
}
}
// λμ΄μ μ
λ°μ΄νΈκ° μλλ©΄ break;
if(!update) break;
}
//nλ²κΉμ§ μ΄€λ¨κ²½λ‘ μ
λ°μ΄νΈκ° μ΄λ£¨μ΄μ§λ€λ©΄
//μμ μ¬μ΄ν΄μ΄ μ‘΄μ¬νλκ²μ΄λΌκ³ νλ¨νλ€.
if(update) {
for (int i = 1; i <= n; i++) {
for (Node n : list.get(i)) {
// μ΄λ κ±°λ¦¬κ° μ€μ΄λ λ€λ©΄, μμ μνμ΄ μ΄λ£¨μ΄μ§κ³ μλκ²μ΄λ€.
if (dist[n.to] > dist[i] + n.time) {
return true;
}
}
}
}
return false;
}
}
μ΄λ €μ λ μ / λ°°μ΄ μ / λλ μ
17λ² μ μΆλ§μ μ±κ³΅νλ€.. μ¬μ€ λ€λ₯Έ λΈλ‘κ·Έλ₯Ό μ‘°κΈ μ°Έμ‘°νλ€...λ€ νκ³ λλκΉ λ²¨λ§ν¬λ μκ³ λ¦¬μ¦μ΄ μ΄ν΄κ° λμλ€.
κ·Έλ¦¬κ³ μ€λ₯κ° λ¬λ ν¬μΈνΈλ
λ΄κ° 벨λ§ν¬λλ₯Ό μ μ΄ν΄ λͺ»ν κ²
a -> bλ‘ κ°λ κ°μ μ΄ νλκ° μλ μλ μλ€λκ²
λλ‘λ μλ°©ν₯μ΄λΌλ κ²
μ¬μ€ 2λ²μ§Έκ²λ§ μ κ²½μ°λ©΄ λλ€. 3λ²μ§Έλ λ΄κ° λ¬Έμ λ₯Ό λμΆ© μ½μκ³ , λ²¨λ§ ν¬λλ...μ...
골λ3μ΄λΌμ μμ μ΄λ €μ λ€...
'Algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Algorithm/Java][νλ‘κ·Έλλ¨Έμ€] λ¬Έμμ΄ μμΆ (0) | 2022.04.10 |
---|---|
[Algorithm/Java][λ°±μ€] 1918 νμ νκΈ°μ (0) | 2022.04.08 |
[Algorithm/Java][νλ‘κ·Έλλ¨Έμ€] νκ² λλ² (0) | 2022.04.06 |
[Algorithm/Java][νλ‘κ·Έλλ¨Έμ€] λ€μ ν° μ«μ (0) | 2022.04.06 |
[Algorithm/Java][λ°±μ€] 1167 νΈλ¦¬μ μ§λ¦ (0) | 2022.04.04 |