Skip to content

[Java] 1967๋ฒˆ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ย #13

@peppermintt0504

Description

@peppermintt0504

๋ฌธ์ œ ํ’€์ด ์›๋ณธ

๋ฌธ์ œ ํ’€์ด

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋กœ ์ ๊ณผ ์  ์‚ฌ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ DFS๋กœ๋„ ๋‹ต์„ ๊ตฌํ•  ์ˆ˜๋Š” ์žˆ์œผ๋‚˜ ๋…ธ๋“œ๊ฐ€ ๋งŽ์•„์ง„๋‹ค๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ตœ๋Œ€ํ•œ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ต์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.


๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ฐ„๋‹จํ•œ ์ดํ•ด๋งŒ ์žˆ์œผ๋ฉด ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์„ ๊ตฌํ•˜๋Š” ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.


  1. ์ž„์˜์˜ ์ ์—์„œ ๊ฐ€์žฅ ๋จผ ์ ์„ ์ฐพ๋Š”๋‹ค.

  2. ํ•ด๋‹น ์ ์—์„œ ๊ฐ€์žฅ ๋จผ ์ ๊ณผ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค. => ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„


ํ•œ ์ ์—์„œ ๋จผ ์ ์€ ๋‹ค๋ฅธ ์ ์—์„œ๋„ ๋ฉ€๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•˜๊ณ  ํ‘ผ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

2023.07.22 - [algorithm/theory] - ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜



ํ’€์ด ์ฝ”๋“œ

import java.io.*;
import java.util.*;
import java.util.stream.IntStream;
 
public class Main {
	public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	public static int INF = 1_000_000;
	public static Map<Integer,ArrayList<Node>> nodes = new HashMap<>();
	public static int T;
	
	
	public static class Node implements Comparable<Node>{
		
		int to;
		int distance;
		
		public Node() {
			
		}

		public Node(int to, int distance) {
			super();
			this.to = to;
			this.distance = distance;
		}

		@Override
		public int compareTo(Node o) {
			
			return this.distance - o.distance;
		}
		
	}
	
	public static void main(String[] args) throws IOException {
		
		T = Integer.parseInt(br.readLine());
		
		if(T == 1) {
			System.out.println(0);
		}else {
			for(int t = 1; t < T; t++) {
				StringTokenizer st =new StringTokenizer(br.readLine());
				
				
				int p1 = Integer.parseInt(st.nextToken());
				int p2 = Integer.parseInt(st.nextToken());
				int distance = Integer.parseInt(st.nextToken());
				
				if(nodes.containsKey(p1)) {
					nodes.get(p1).add(new Node(p2,distance));
				}else {
					ArrayList<Node> temp = new ArrayList<>();
					temp.add(new Node(p2,distance));
					nodes.put(p1,temp);
				}
				if(nodes.containsKey(p2)) {
					nodes.get(p2).add(new Node(p1,distance));
				}else {
					ArrayList<Node> temp = new ArrayList<>();
					temp.add(new Node(p1,distance));
					nodes.put(p2,temp);
				}
				
			}
				int[] first = djikstra(1);
				int max = Arrays.stream(first).reduce(0, (x,v)-> x > v ? x : v);
				int maxIndex = IntStream.range(0, first.length).
		                filter(i -> max == first[i]).
		                findFirst().orElse(-1);
				int[] second = djikstra(maxIndex);
				int answer = Arrays.stream(second).reduce(0, (x,v)-> x > v ? x : v);
				
				System.out.println(answer);
			bw.flush();
			bw.close();
		}
		
	}

	public static int[] djikstra(int start) {
		int[] dp = new int[T+1];
		boolean[] visited= new boolean[T+1];
		PriorityQueue<Node> pq = new PriorityQueue<>();
		
		Arrays.fill(dp, INF);
		dp[0] = 0;
		dp[start] = 0;
		pq.add(new Node(start,0));
		
		while(!pq.isEmpty()) {
			Node cur = pq.poll();
			
			if(visited[cur.to]) continue;
			visited[cur.to] = true;
			
			for(Node next : nodes.get(cur.to)) {
				if(dp[next.to] > dp[cur.to] + next.distance) {
					dp[next.to] = dp[cur.to] + next.distance;
					pq.add(new Node(next.to,dp[next.to]));
				}
			}
		}
		return dp;
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions