Skip to content

Commit 00c7f24

Browse files
committed
[Gold IV] Title: 운동, Time: 516 ms, Memory: 58828 KB -BaekjoonHub
1 parent 03a543e commit 00c7f24

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Gold IV] 운동 - 1956
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1956)
4+
5+
### 성능 요약
6+
7+
메모리: 58828 KB, 시간: 516 ms
8+
9+
### 분류
10+
11+
그래프 이론, 최단 경로, 플로이드–워셜
12+
13+
### 제출 일자
14+
15+
2025년 12월 22일 16:47:35
16+
17+
### 문제 설명
18+
19+
<p>V개의 마을와 E개의 도로로 구성되어 있는 도시가 있다. 도로는 마을과 마을 사이에 놓여 있으며, 일방 통행 도로이다. 마을에는 편의상 1번부터 V번까지 번호가 매겨져 있다고 하자.</p>
20+
21+
<p>당신은 도로를 따라 운동을 하기 위한 경로를 찾으려고 한다. 운동을 한 후에는 다시 시작점으로 돌아오는 것이 좋기 때문에, 우리는 사이클을 찾기를 원한다. 단, 당신은 운동을 매우 귀찮아하므로, 사이클을 이루는 도로의 길이의 합이 최소가 되도록 찾으려고 한다.</p>
22+
23+
<p>도로의 정보가 주어졌을 때, 도로의 길이의 합이 가장 작은 사이클을 찾는 프로그램을 작성하시오. 두 마을을 왕복하는 경우도 사이클에 포함됨에 주의한다.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 V와 E가 빈칸을 사이에 두고 주어진다. (2 ≤ V ≤ 400, 0 ≤ E ≤ V(V-1)) 다음 E개의 줄에는 각각 세 개의 정수 a, b, c가 주어진다. a번 마을에서 b번 마을로 가는 거리가 c인 도로가 있다는 의미이다. (a → b임에 주의) 거리는 10,000 이하의 자연수이다. (a, b) 쌍이 같은 도로가 여러 번 주어지지 않는다.</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 최소 사이클의 도로 길이의 합을 출력한다. 운동 경로를 찾는 것이 불가능한 경우에는 -1을 출력한다.</p>
32+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
private static final int INF = (int)1e9;
6+
7+
public static void main(String[] args) throws IOException{
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
12+
int n = Integer.parseInt(st.nextToken());
13+
int m = Integer.parseInt(st.nextToken());
14+
15+
int[][] graph = new int[n+1][n+1];
16+
17+
for(int i=0; i<=n; i++) {
18+
Arrays.fill(graph[i], INF);
19+
graph[i][i] = 0;
20+
}
21+
22+
for(int i=0; i<m; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
25+
int a = Integer.parseInt(st.nextToken());
26+
int b = Integer.parseInt(st.nextToken());
27+
int c = Integer.parseInt(st.nextToken());
28+
29+
graph[a][b] = c;
30+
}
31+
32+
for(int k=1; k<=n; k++) {
33+
for(int i=1; i<=n; i++) {
34+
for(int j=1; j<=n; j++) {
35+
graph[i][j] = Math.min(graph[i][j], graph[i][k]+graph[k][j]);
36+
}
37+
}
38+
}
39+
40+
int min = Integer.MAX_VALUE;
41+
for(int i=1; i<=n; i++) {
42+
for(int j=i+1; j<=n; j++) {
43+
if(graph[i][j] == INF || graph[j][i] == INF) continue;
44+
min = Math.min(min, graph[i][j] + graph[j][i]);
45+
}
46+
}
47+
48+
if(min == Integer.MAX_VALUE) min = -1;
49+
System.out.println(min);
50+
}
51+
52+
private static class Edge {
53+
int to;
54+
int cost;
55+
56+
public Edge(int to, int cost) {
57+
this.to = to;
58+
this.cost = cost;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)