https://www.acmicpc.net/problem/1956
1956번: 운동
첫째 줄에 V와 E가 빈칸을 사이에 두고 주어진다. (2 ≤ V ≤ 400, 0 ≤ E ≤ V(V-1)) 다음 E개의 줄에는 각각 세 개의 정수 a, b, c가 주어진다. a번 마을에서 b번 마을로 가는 거리가 c인 도로가 있다는 의
www.acmicpc.net
문제 풀이(코드)
import java.util.*;
public class Main {
static int[][] graph;
static int v, e, min = Integer.MAX_VALUE;
static final int INF = (int)1e9;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
v = scan.nextInt();
e= scan.nextInt();
graph = new int[v+1][v+1];
for(int i = 0; i <= v; i++)
Arrays.fill(graph[i],INF);
for(int i = 0; i < e; i++) {
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
graph[a][b] = Math.min(graph[a][b], c);
}
for(int k = 1; k <= v; k++)
for(int i = 1; i <= v; i++)
for(int j = 1; j <= v; j++)
graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
for(int i = 1; i <= v; i++)
min = Math.min(min, graph[i][i]);
if(min == INF)
System.out.println(-1);
else
System.out.println(min);
scan.close();
}
}
'알고리즘' 카테고리의 다른 글
[백준 BOJ][JAVA] 11725번: 트리의 부모 찾기 (0) | 2022.02.11 |
---|---|
[백준 BOJ][JAVA] 1991번: 트리 순회 (0) | 2022.02.06 |
[백준 BOJ][JAVA] 7569번: 토마토 (0) | 2022.02.04 |
[백준 BOJ][JAVA] 2606번: 바이러스 (0) | 2022.01.30 |
[백준 BOJ][JAVA] 10886번: 덱 (0) | 2022.01.29 |