๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€

[๋ฐฑ์ค€ ์Šคํ„ฐ๋”” 5์ฃผ์ฐจ] 1719 ํƒ๋ฐฐ ๊ณจ๋“œ4 - ์ž๋ฐ”(Java) / ๋‹ค์ต์ŠคํŠธ๋ผ

๐Ÿ“‘ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1719

 

1719๋ฒˆ: ํƒ๋ฐฐ

๋ช…์šฐ๊ธฐ์—…์€ 2008๋…„๋ถ€ํ„ฐ ํƒ๋ฐฐ ์‚ฌ์—…์„ ์ƒˆ๋กœ์ด ์‹œ์ž‘ํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ์šฐ์„  ํƒ๋ฐฐ ํ™”๋ฌผ์„ ๋ชจ์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ์ง‘ํ•˜์žฅ์„ ๋ช‡ ๊ฐœ ๋งˆ๋ จํ–ˆ์ง€๋งŒ, ํƒ๋ฐฐ ํ™”๋ฌผ์ด ๊ฐ ์ง‘ํ•˜์žฅ๋“ค ์‚ฌ์ด๋ฅผ ์˜ค๊ฐˆ ๋•Œ ์–ด๋–ค ๊ฒฝ๋กœ๋ฅผ ๊ฑฐ์ณ์•ผ ํ•˜

www.acmicpc.net

๋”๋ณด๊ธฐ

๋ฌธ์ œ

๋ช…์šฐ๊ธฐ์—…์€ 2008๋…„๋ถ€ํ„ฐ ํƒ๋ฐฐ ์‚ฌ์—…์„ ์ƒˆ๋กœ์ด ์‹œ์ž‘ํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ์šฐ์„  ํƒ๋ฐฐ ํ™”๋ฌผ์„ ๋ชจ์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ์ง‘ํ•˜์žฅ์„ ๋ช‡ ๊ฐœ ๋งˆ๋ จํ–ˆ์ง€๋งŒ, ํƒ๋ฐฐ ํ™”๋ฌผ์ด ๊ฐ ์ง‘ํ•˜์žฅ๋“ค ์‚ฌ์ด๋ฅผ ์˜ค๊ฐˆ ๋•Œ ์–ด๋–ค ๊ฒฝ๋กœ๋ฅผ ๊ฑฐ์ณ์•ผ ํ•˜๋Š”์ง€ ๊ฒฐ์ •ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ์–ด๋–ค ๊ฒฝ๋กœ๋ฅผ ๊ฑฐ์น ์ง€ ์ •ํ•ด์„œ, ์ด๋ฅผ ๊ฒฝ๋กœํ‘œ๋กœ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์—ฌ๋Ÿฌ๋ถ„์ด ํ•  ์ผ์ด๋‹ค.

์˜ˆ์‹œ๋œ ๊ทธ๋ž˜ํ”„์—์„œ ๊ตต๊ฒŒ ํ‘œ์‹œ๋œ 1, 2, 3, 4, 5, 6์€ ์ง‘ํ•˜์žฅ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ •์ ๊ฐ„์˜ ๊ฐ„์„ ์€ ๋‘ ์ง‘ํ•˜์žฅ๊ฐ„์— ํ™”๋ฌผ ์ด๋™์ด ๊ฐ€๋Šฅํ•จ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๊ฐ€์ค‘์น˜๋Š” ์ด๋™์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด๋‹ค. ์ด๋กœ๋ถ€ํ„ฐ ์–ป์–ด๋‚ด์•ผ ํ•˜๋Š” ๊ฒฝ๋กœํ‘œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๊ฒฝ๋กœํ‘œ๋Š” ํ•œ ์ง‘ํ•˜์žฅ์—์„œ ๋‹ค๋ฅธ ์ง‘ํ•˜์žฅ์œผ๋กœ ์ตœ๋‹จ๊ฒฝ๋กœ๋กœ ํ™”๋ฌผ์„ ์ด๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ๋จผ์ € ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ์ง‘ํ•˜์žฅ์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 4ํ–‰ 5์—ด์˜ 6์€ 4๋ฒˆ ์ง‘ํ•˜์žฅ์—์„œ 5๋ฒˆ ์ง‘ํ•˜์žฅ์œผ๋กœ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ œ์ผ ๋จผ์ € 6๋ฒˆ ์ง‘ํ•˜์žฅ์œผ๋กœ ์ด๋™ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

์ด์™€ ๊ฐ™์€ ๊ฒฝ๋กœํ‘œ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋‘ ์ˆ˜ n๊ณผ m์ด ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. n์€ ์ง‘ํ•˜์žฅ์˜ ๊ฐœ์ˆ˜๋กœ 200์ดํ•˜์˜ ์ž์—ฐ์ˆ˜, m์€ ์ง‘ํ•˜์žฅ๊ฐ„ ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜๋กœ 10000์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์ด์–ด์„œ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ง‘ํ•˜์žฅ๊ฐ„ ๊ฒฝ๋กœ๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ๋‘ ์ง‘ํ•˜์žฅ์˜ ๋ฒˆํ˜ธ์™€ ๊ทธ ์‚ฌ์ด๋ฅผ ์˜ค๊ฐ€๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์ด ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ง‘ํ•˜์žฅ์˜ ๋ฒˆํ˜ธ๋“ค๊ณผ ๊ฒฝ๋กœ์˜ ์†Œ์š”์‹œ๊ฐ„์€ ๋ชจ๋‘ 1000์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

์˜ˆ์‹œ๋œ ๊ฒƒ๊ณผ ๊ฐ™์€ ํ˜•์‹์˜ ๊ฒฝ๋กœํ‘œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

 

 

 

 

โœ’๏ธ ํ’€์ด

 ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๋งค ์ง‘ํ•˜์žฅ๋งˆ๋‹ค ๋Œ์•„์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ํ’€์—ˆ๋‹ค.

 N๊ฐœ์˜ ์ง‘ํ•˜์žฅ ์ค‘ ํ•˜๋‚˜์—์„œ, ๋‚จ์€ ๋‹ค๋ฅธ N-1๊ฐœ์˜ ์ง‘ํ•˜์žฅ ์ค‘ ํ•˜๋‚˜๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœํ‘œ๋ฅผ ๋ชจ๋‘ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋ฏ€๋กœ, ์ด ์ฝ”๋“œ๋Š” ์ด ์„ธ ๋ฒˆ์˜ ๋ฃจํ”„๋ฅผ ๋Œ๊ฒŒ ํ’€์—ˆ๋‹ค.

1๏ธโƒฃ ์ถœ๋ฐœ ์ง‘ํ•˜์žฅ์„ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ ์ง‘ํ•˜์žฅ๊นŒ์ง€ ์„ค์ •ํ•˜๋Š” for๋ฌธ

2๏ธโƒฃ (๋‹ค์ต์ŠคํŠธ๋ผ) ๋งค ์ƒํƒœ๋งˆ๋‹ค ์ตœ์†Œ ๊ฐ€์ค‘์น˜ ๋…ธ๋“œ(current๋กœ ์„ค์ •ํ•  ๋…ธ๋“œ)๋ฅผ ์ฐพ๋Š” for๋ฌธ.

3๏ธโƒฃ (๋‹ค์ต์ŠคํŠธ๋ผ) 2๋ฒˆ์—์„œ ์ฐพ์€ ์ตœ์†Œ๊ฐ€์ค‘์น˜ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” N-1๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ, ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” for๋ฌธ.

 

 


 

 >> ์ €์žฅ ๋ณ€์ˆ˜

๐Ÿ—‚๏ธ int[][] containers: ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•˜๋Š” ์ธ์ ‘ํ–‰๋ ฌ ๋ฐฐ์—ด.

๐Ÿ—‚๏ธ int[][] outputs: ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅํ•  ๊ฒฝ๋กœํ‘œ. output[i][j]๋Š” i๋ฒˆ ์ง‘ํ•˜์žฅ์—์„œ j๋ฒˆ ์ง‘ํ•˜์žฅ์œผ๋กœ ๊ฐˆ ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ๊ฑฐ์น˜๋Š” ์ง‘ํ•˜์žฅ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

 >> ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๐Ÿ—‚๏ธ boolean[][] visited:

๐Ÿ—‚๏ธ int[] temp: 1๏ธโƒฃ๋ฒˆ ๋ฃจํ”„์—์„œ ์„ค์ •๋œ ์ถœ๋ฐœ ์ง‘ํ•˜์žฅ์ด ๋‹ค๋ฅธ ์ง‘ํ•˜์žฅ๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ด์„ ๋ณ€์ˆ˜. ๋ฃจํ”„ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ๋ฏธ๋ฆฌ ์ธํ’‹์œผ๋กœ ๋ฐ›์€ ๊ฐ€์ค‘์น˜๋ฅผ containers ๋ณ€์ˆ˜์—์„œ ๋ณต์‚ฌํ•ด์˜จ๋‹ค.  

 


 

 

 โ—ป๏ธ 1๏ธโƒฃ๋ฒˆ ๋ฃจํ”„์—์„œ ๋งค ์ถœ๋ฐœ ์ง‘ํ•˜์žฅ์„ current ๋…ธ๋“œ(์‹œ์ž‘ ๋…ธ๋“œ)๋กœ ์„ค์ •ํ•˜๊ณ , ๊ฐ€์ค‘์น˜ ๋ณ€์ˆ˜ temp๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

 โ—ป๏ธ current ๋…ธ๋“œ(์ตœ์†Œ๊ฐ€์ค‘์น˜ ๋…ธ๋“œ)์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์„ ์‚ดํŽด๋ณธ๋‹ค. (3๏ธโƒฃ๋ฒˆ ๋ฃจํ”„)

 

 โ—ป๏ธ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์€ ๊ฑฐ๊ธฐ๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ๋กœ ๋น„์šฉ์ด ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋กœ ์—…๋ฐ์ดํŠธ ๋˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

 

 โ—ป๏ธ ๋งŒ์•ฝ current๋…ธ๋“œ๊ฐ€ ์ถœ๋ฐœ ์ง‘ํ•˜์žฅ(1๏ธโƒฃ๋ฒˆ ๋ฃจํ”„)์ด๋ฉด, current ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ(k)๋“ค์€ ์ถœ๋ฐœ ์ง‘ํ•˜์žฅ์ด ๊ฐ€์žฅ ๋จผ์ € ๊ฑฐ์น˜๋Š” ์ง‘ํ•˜์žฅ์ด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋…ธ๋“œ๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ output[์ถœ๋ฐœ์ง‘ํ•˜์žฅ=current][k] ๋ฐฐ์—ด์— ๋„ฃ์–ด์ค€๋‹ค.

 

 โ—ป๏ธ ๋งŒ์•ฝ current๋…ธ๋“œ๊ฐ€ ์ถœ๋ฐœ ์ง‘ํ•˜์žฅ์ด ์•„๋‹ˆ๋ฉด, ์—ฌ๊ธฐ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ(k)๋“ค์€ output[์ถœ๋ฐœ์ง‘ํ•˜์žฅ][current]์— ์ €์žฅ๋œ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ output[์ถœ๋ฐœ์ง‘ํ•˜์žฅ][k]์— ๋„ฃ์–ด์ค€๋‹ค.

 ์˜ˆ๋ฅผ ๋“ค์–ด 1๋ฒˆ ์ง‘ํ•˜์žฅ์—์„œ 2๋ฒˆ>5๋ฒˆ>6๋ฒˆ ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ๊ฐ„๋‹ค๊ณ  ํ•  ๋•Œ, 1๋ฒˆ ์ง‘ํ•˜์žฅ์—์„œ 5๋ฒˆ, 6๋ฒˆ ์ง‘ํ•˜์žฅ์œผ๋กœ ๊ฐ€๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ๋จผ์ € ๊ฑฐ์น˜๋Š” ์ง‘ํ•˜์žฅ์€ ๋ชจ๋‘ 2๋ฒˆ ์ง‘ํ•˜์žฅ์ด ๋˜๋ฏ€๋กœ, output[1][2]=2 >> output[1][5]=output[1][2] >> output[1][6]=output[1][5]๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

 โ—ป๏ธ 3๏ธโƒฃ๋ฒˆ ๋ฃจํ”„๊ฐ€ ๋๋‚˜๋ฉด ๊ฒฝ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ์ œ์ผ ์ž‘์€ ๋…ธ๋“œ๊ฐ€ ๋‹ค์Œ์— ํƒ์ƒ‰ํ•  current ๋…ธ๋“œ๊ฐ€ ๋œ๋‹ค.(2๏ธโƒฃ๋ฒˆ ๋ฃจํ”„ ๋ฐ˜๋ณต)

 

 

 

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ์˜๊ฒฌ

 ๋‹ค์ต์ŠคํŠธ๋ผ์˜ ๊ฐœ๋…์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋‹ค์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

 ํšจ์œจ์„ฑ์€...........์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ํ”Œ๋ฃจ์ด๋“œ-์›Œ์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ๋‚œ ๋‹ค์ต์ŠคํŠธ๋ผ ์›๊ธธ์„ ๊ฑท๊ธฐ์—(ใ…Žใ…Ž)

 

 

 

 

 

 

 

 

๐Ÿ’ป ์†Œ์Šค์ฝ”๋“œ: ์ž๋ฐ”(Java)

public class Main_๋ฐฑ์ค€_1719_ํƒ๋ฐฐ_๊ณจ๋“œIV {

	static boolean[] visited;
	static int N, M;
	
	public static void main(String[] args) throws IOException {
	
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		int[][] containers = new int[N+1][N+1];
		int[][] outputs = new int[N+1][N+1];
		visited = new boolean[N+1];
	
		for (int i = 0; i < M; i++) {
			st = new StringTokenizer(br.readLine());
			int v1 = Integer.parseInt(st.nextToken());
			int v2 = Integer.parseInt(st.nextToken());
			int weight = Integer.parseInt(st.nextToken());
			containers[v1][v2] = containers[v2][v1] = weight;
		}// input
		
	
		for (int i = 1; i <= N; i++) {	//๊ฐ ์‹œ์ž‘์ (ํ–‰)์— ๋Œ€ํ•˜์—ฌ
			int current=i;
			int[] temp = Arrays.copyOf(containers[i], N+1);

			for (int j = 1; j <= N; j++) {	// N๊ฐœ์˜ current ๋…ธ๋“œ ํ™•์ธ
				int min=Integer.MAX_VALUE, minVertex=current;
				visited[current]=true;
				
				for (int k = 1; k <= N; k++) {	// current๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ๊ฒƒ๋“ค ํ™•์ธ	
					if(containers[current][k]==0 || visited[k]) continue;
					if(containers[current][k] != 0 && !visited[k]) {
						if(current == i) {
							outputs[i][k] = k;
						}else {
							if(temp[k]==0) {
								temp[k] = temp[current] + containers[current][k];
								outputs[i][k] = outputs[i][current];								
							}else {
								if(temp[current] + containers[current][k] < temp[k]) {
									temp[k] = temp[current] + containers[current][k];
									outputs[i][k] = outputs[i][current];
								}
							}
						}
					}
				}//์ตœ์†Œ๋น„์šฉ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค

				// ์ตœ์†Œ๋น„์šฉ๋…ธ๋“œ๋ฅผ current๋กœ ์—…๋ฐ์ดํŠธ
				for (int t = 1; t <= N; t++) {
					if(t==current || visited[t]) continue;
					if(temp[t]!=0 && temp[t] < min) {
						min = temp[t];
						minVertex = t;
					}
				}		
				current = minVertex;

			}
			clearIsVisited();
		}

		
		for (int i = 1; i <= N; i++) {	//print
			for (int j = 1; j <= N; j++) {
				if(i==j) System.out.print("- ");
				else System.out.print(outputs[i][j]+" ");
			}
			System.out.println();
		}
		
	}//end of main
	
	static void clearIsVisited() {
		for (int i = 1; i <= N; i++) {
			visited[i] = false;
		}
	}
}//end of class

 

 

 

 

 

 

 

 

 

๐Ÿ’Ž ๐Ÿ’Ž ๐Ÿ’Ž

 

 

๏ผฐ๏ฝ๏ฝ“๏ฝ”๏ฝ…๏ฝ„ ๏ผข๏ฝ™ ๏ผณ๏ผก๏ผน

๐˜›๐˜ฉ๐˜ข๐˜ฏ๐˜ฌ๐˜ด ๐˜ง๐˜ฐ๐˜ณ ๐˜ณ๐˜ฆ๐˜ข๐˜ฅ๐˜ช๐˜ฏ๐˜จ