๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1865_์›œํ™€

๋ฟŒ์•ผ._. 2024. 1. 12. 13:52

Gold III

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

< ์›œํ™€ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

์ฒ˜์Œ์—๋Š” ๋ฐ์ดํฌ์ŠคํŠธ๋ผ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•˜์ง€๋งŒ ์Œ์ˆ˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋ฐ์ดํฌ์ŠคํŠธ๋ผ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋‚˜ ์ฐพ์•„๋ณด๋‹ˆ ์Œ์ˆ˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด์„œ ๋ฐ์ดํฌ์ŠคํŠธ๋ผ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์‚ฌ์ดํด ํ˜•์„ฑ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์‚ฌ์šฉ ์—ฌ๋ถ€๊ฐ€ ๊ฐˆ๋ฆฌ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class _1865_ { // ์›œํ™€
	static class Edge {
		private int start;
		private int end;
		private int d;

		public Edge(int start, int end, int d) {
			this.start = start;
			this.end = end;
			this.d = d;
		}
	}

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

		int t = Integer.parseInt(bf.readLine());

		ArrayList<Edge> list = new ArrayList<>();
		int result[];
		boolean visited[];
		boolean flag = false;

		for (int i = 0; i < t; i++) {
			st = new StringTokenizer(bf.readLine());
			int n = Integer.parseInt(st.nextToken());
			int m = Integer.parseInt(st.nextToken());
			int w = Integer.parseInt(st.nextToken());

			result = new int[n + 1];
			visited = new boolean[n + 1];
			flag = false;

			for (int j = 0; j < m; j++) {
				st = new StringTokenizer(bf.readLine());
				int s = Integer.parseInt(st.nextToken());
				int e = Integer.parseInt(st.nextToken());
				int d = Integer.parseInt(st.nextToken());
				list.add(new Edge(s, e, d));
				list.add(new Edge(e, s, d));
			}

			for (int j = 0; j < w; j++) {
				st = new StringTokenizer(bf.readLine());
				int s = Integer.parseInt(st.nextToken());
				int e = Integer.parseInt(st.nextToken());
				int d = Integer.parseInt(st.nextToken());
				list.add(new Edge(s, e, -d));
			}

			for (int j = 1; j <= n; j++) {
				if (visited[j]) {
					continue;
				}
				for (int k = 1; k <= n; k++) {
					result[k] = Integer.MAX_VALUE;
				}
				visited[j] = true;
				result[j] = 0;

				for (int k = 0; k < n; k++) {
					for (int v = 0; v < list.size(); v++) {
						Edge edge = list.get(v);
						if (result[edge.start] != Integer.MAX_VALUE && result[edge.end] > result[edge.start] + edge.d) {
							result[edge.end] = result[edge.start] + edge.d;
							visited[edge.end] = true;
							if (k == n - 1) {
								flag = true;
								break;
							}
						}
					}
					if (flag) {
						break;
					}
				}
				if (flag) {
					break;
				}
			}
			if (flag) {
				bw.write("YES\n");
			} else {
				bw.write("NO\n");
			}
			list.clear();
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
Edge : ์‹œ์ž‘ ์ง€์ , ๋„์ฐฉ ์ง€์ , ์‹œ๊ฐ„
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜
list : ๊ทธ๋ž˜ํ”„
result : ์ด๋™ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
flag : ์‹œ๊ฐ„์ด ์ค„์–ด๋“ค๋ฉด์„œ ์ถœ๋ฐœ ์œ„์น˜๋กœ ๋Œ์•„์˜ค๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ์ง€ ์—ฌ๋ถ€
n, m, w : ์ง€์ ์˜ ์ˆ˜, ๋„๋กœ์˜ ๊ฐœ์ˆ˜, ์›œํ™€์˜ ๊ฐœ์ˆ˜
s, e, d : ์‹œ์ž‘ ์ง€์ , ๋„์ฐฉ ์ง€์ , ์ค„์–ด๋“œ๋Š” ์‹œ๊ฐ„

 

Edge

์‹œ์ž‘ ์ง€์ , ๋„์ฐฉ ์ง€์ , ์‹œ๊ฐ„์„ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง

 

Main

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋งŒํผ ์•„๋ž˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

์ง€์ ์˜ ์ˆ˜, ๋„๋กœ์˜ ๊ฐœ์ˆ˜, ์›œํ™€์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋„๋กœ์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋„๋กœ์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ์›œํ™€์˜ ๊ฐœ์ˆ˜๋งŒํผ ์›œํ™€์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ์ถœ๋ฐœ ์ง€์ ์ด ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ชจ๋“  ์ง€์ ์—์„œ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์ง€์ ์˜ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋งŒ์•ฝ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์ด๋ผ๋ฉด ๋‹ค์Œ ์ง€์ ์„ ํƒ์ƒ‰ํ•œ๋‹ค. ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ๊ฐ ์ง€์ ์˜ ์‹œ๊ฐ„์„ Integer.MAX_VALUE๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์‹œ์ž‘ ์ง€์ ์˜ ์‹œ๊ฐ„์„ 0์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ์ง€์ ์˜ ์ˆ˜๋งŒํผ ๋ชจ๋“  ๋„๋กœ์™€ ์›œํ™€์„ ํƒ์ƒ‰ํ•ด ๋ณด๋ฉฐ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์•ฝ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋  ๋•Œ๊ฐ€ (์ง€์ ์˜ ์ˆ˜-1) ๋ฒˆ์งธ ๋ผ๋ฉด ์Œ์ˆ˜ ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ข…๋ฃŒํ•œ๋‹ค.

 

์Œ์ˆ˜ ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ–ˆ๋‹ค๋ฉด YES๋ฅผ, ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด NO๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.