Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 1238. 파티 본문

acmicpc

[BAEKJOON] 1238. 파티

코드와이 2021. 4. 12. 17:39

 

문제링크

www.acmicpc.net/problem/1238

 

1238번: 파티

첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), X가 공백으로 구분되어 입력된다. 두 번째 줄부터 M+1번째 줄까지 i번째 도로의 시작점, 끝점, 그리고 이 도로를 지나는데 필요한 소요시간 Ti가 들어

www.acmicpc.net

 

package acmicpc.Gold3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class 파티{

	static class Point implements Comparable<Point>{
		int to;
		int weight;
		
		public Point(int to, int weight) {
			super();
			this.to = to;
			this.weight = weight;
		}

		@Override
		public int compareTo(Point o) {
			return this.weight - o.weight;
		}
	}
	
	static int n, m, x;
	static Point[] arr;
	
	public static void main(String[] args) throws NumberFormatException, 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());
		x = Integer.parseInt(st.nextToken());
		
		ArrayList<Point>[]arr = new ArrayList[n+1];
		
		for(int i = 0 ; i <= n ; i++) {
			arr[i] = new ArrayList<Point>();
		}
		
		for(int i = 0 ; i < m ; i++) {
			st = new StringTokenizer(br.readLine());
			
			int f = Integer.parseInt(st.nextToken());
			int t = Integer.parseInt(st.nextToken());
			int w = Integer.parseInt(st.nextToken());
			
			arr[f].add(new Point(t,w));
		}
		
		int[] sum = new int[n+1];
		for(int num = 1 ; num <= n ; num++) {
			Queue<Point> queue = new LinkedList<Point>();
			queue.add(new Point(num,0));
			
			int[] d = new int[n+1];
			Arrays.fill(d, 987654321);
			d[num] = 0;
			
			while(!queue.isEmpty()) {
				
				Point q = queue.poll();
				int to = q.to;
				
				for(int i = 0 ; i < arr[to].size() ; i++) {
					Point n = arr[to].get(i);
					if(d[to] + n.weight < d[n.to]) {
						d[n.to] = d[to] + n.weight;
						queue.add(new Point(n.to, n.weight));
					}
				}
				
			}
			
			if(x == num) {
				for(int i = 1 ; i <= n ; i++) {
					sum[i] += d[i];
				}
			} else {
				sum[num] += d[x];
			}
		}

		int ans = 0;
		for(int a : sum) ans = Math.max(a, ans);
		System.out.println(ans);
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 14501. 퇴사  (0) 2021.04.13
[BAEKJOON] 1010. 다리놓기  (0) 2021.04.13
[BAEKJOON] 1261. 알고스팟  (0) 2021.04.12
[BAEKJOON] 1916. 최소비용 구하기  (0) 2021.04.12
[BAEKJOON] 10844. 쉬운 계단 수  (0) 2021.04.06