๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1956_์šด๋™

๋ฟŒ์•ผ._. 2023. 10. 20. 17:12

Gold IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1956)

< ์šด๋™ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋จผ์ € a๋ฒˆ ๋งˆ์„์—์„œ b๋ฒˆ ๋งˆ์„๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์€ ํ›„ b๋ฒˆ ๋งˆ์„์—์„œ ๋‹ค์‹œ a๋ฒˆ์œผ๋กœ ์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class _1956_ { // ์šด๋™

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int v = Integer.parseInt(st.nextToken());
		int e = Integer.parseInt(st.nextToken());

		int arr[][] = new int[v][v];
		for (int i = 0; i < v; i++) {
			for (int j = 0; j < v; j++) {
				if (i == j)
					arr[i][j] = 0;
				else
					arr[i][j] = Integer.MAX_VALUE;
			}
		}

		for (int i = 0; i < e; i++) {
			st = new StringTokenizer(bf.readLine());

			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());

			arr[a - 1][b - 1] = c;
		}

		// [์‹œ์ž‘, ๋] = [์‹œ์ž‘, ์ค‘๊ฐ„]+[์ค‘๊ฐ„, ๋]
		for (int i = 0; i < v; i++) {
			for (int j = 0; j < v; j++) {
				for (int k = 0; k < v; k++) {
					if (arr[j][i] == Integer.MAX_VALUE || arr[i][k] == Integer.MAX_VALUE)
						continue;
					arr[j][k] = Math.min(arr[j][k], arr[j][i] + arr[i][k]);
				}
			}
		}

		int result = Integer.MAX_VALUE;

		for (int i = 0; i < v; i++) {
			for (int j = 0; j < v; j++) {
				if (i == j || arr[i][j] == Integer.MAX_VALUE || arr[j][i] == Integer.MAX_VALUE)
					continue;

				if (arr[i][i] != 0)
					arr[i][i] = Math.min(arr[i][i], arr[i][j] + arr[j][i]);
				else
					arr[i][i] = arr[i][j] + arr[j][i];

				result = Math.min(result, arr[i][i]);
			}
		}

		if(result==Integer.MAX_VALUE)
			System.out.println(-1);
		else
			System.out.println(result);
	}
}

 

Main

๋ณ€์ˆ˜)
v, e : ๋งˆ์„ ๊ฐœ์ˆ˜, ๋„๋กœ ๊ฐœ์ˆ˜
arr : ์ตœ๋‹จ ๊ฑฐ๋ฆฌ
a, b, c : a๋ฒˆ ๋งˆ์„์—์„œ b๋ฒˆ ๋งˆ์„๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ c
result : ์ตœ์†Œ ์‚ฌ์ดํด์˜ ๋„๋กœ ๊ธธ์ด์˜ 

 

- ๋งˆ์„ ๊ฐœ์ˆ˜(v), ๋„๋กœ ๊ฐœ์ˆ˜(e) ์ž…๋ ฅ

- a๋ฒˆ ๋งˆ์„์—์„œ b๋ฒˆ ๋งˆ์„๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ c ์ €์žฅ

- [์‹œ์ž‘ ๋งˆ์„, ๋ ๋งˆ์„] = [์‹œ์ž‘ ๋งˆ์„, ์ค‘๊ฐ„ ๋งˆ์„]+[์ค‘๊ฐ„ ๋งˆ์„, ๋ ๋งˆ์„]์„ ๊ตฌํ•จ

- ์‚ฌ์ดํด์„ ์ฐพ์•„์•ผ ํ•˜๋ฏ€๋กœ [์‹œ์ž‘ ๋งˆ์„, ์‹œ์ž‘ ๋งˆ์„] = [์‹œ์ž‘ ๋งˆ์„, ๋ ๋งˆ์„] + [๋ ๋งˆ์„, ์‹œ์ž‘ ๋งˆ์„]์ธ ๊ฒƒ์„ ์ฐพ์œผ๋ฉด์„œ ์ตœ์†Œ ์‚ฌ์ดํด์˜ ๋„๋กœ ๊ธธ์ด ํ•ฉ์„ ๊ตฌํ•จ 

- ์ตœ์†Œ ์‚ฌ์ดํด์˜ ๋„๋กœ ๊ธธ์ด ํ•ฉ(result) ์ถœ๋ ฅ



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 9575_ํ–‰์šด์˜ ์ˆ˜  (1) 2023.10.24
[Baekjoon] 1058_์นœ๊ตฌ  (1) 2023.10.23
[Baekjoon] 1015_์ˆ˜์—ด ์ •๋ ฌ  (0) 2023.10.19
[Baekjoon] 20044_Project Teams  (0) 2023.10.18
[Baekjoon] 2548_๋Œ€ํ‘œ ์ž์—ฐ์ˆ˜  (0) 2023.10.17