1238. 파티 (Java)

2023. 10. 24. 11:37·Algorithm & Data Structures/BOJ

 다익스트라 응용 문제

package BOJ;

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

class Node implements Comparable<Node>{
	int end, val;

	public Node(int end, int val) {
		super();
		this.end = end;
		this.val = val;
	}

	@Override	
	public int compareTo(Node o) {
		return this.val - o.val;
	}
	
}

public class Main {

	static int N,M,X;
	static ArrayList<Node>[] list;
	static int[] dist , ans;
	static boolean[] isVisited;
	
	public static int dijkstra(int s, int e) {
		PriorityQueue<Node> pq = new PriorityQueue<>();
		pq.add(new Node(s,0));
		dist[s] = 0;
		while(!pq.isEmpty()) {
			Node now = pq.poll();
			if(isVisited[now.end]) continue;
			isVisited[now.end] = true;
			for(Node n : list[now.end]) {
				if(dist[n.end] > dist[now.end]+n.val) {
					dist[n.end] = dist[now.end]+n.val;
					pq.add(new Node(n.end,dist[n.end]));
				}
			}	
		}
		return dist[e];
	}
		
	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());
		X = Integer.parseInt(st.nextToken());
		
		list = new ArrayList[N+1];
		
		for(int i = 0 ; i <= N ; i++) {
			list[i] = new ArrayList<>();
		}
		
		for(int i = 0 ; i < M ; i++) {
			st = new StringTokenizer(br.readLine());
			int s = Integer.parseInt(st.nextToken());
			int e = Integer.parseInt(st.nextToken());
			int v = Integer.parseInt(st.nextToken());
			list[s].add(new Node(e,v));
		}
		
		ans = new int[N+1];
		
		for(int i = 1 ; i <= N ; i++) {
			if(X==i) continue;
			dist = new int[N+1];
			Arrays.fill(dist, Integer.MAX_VALUE);
			isVisited = new boolean[N+1];
			ans[i]+=dijkstra(i,X);
			
			dist = new int[N+1];
			Arrays.fill(dist, Integer.MAX_VALUE);
			isVisited = new boolean[N+1];
			ans[i]+=dijkstra(X,i);
		}
		int max = Integer.MIN_VALUE;
		for(int a : ans) {
			if(max < a) {
				max = a;
			}
		}
		
		System.out.println(max);
	}
}
/*

 */

 

'Algorithm & Data Structures > BOJ' 카테고리의 다른 글

1504. 특정한 최단경로 (Java)  (0) 2023.10.31
2294. 동전2 (Java)  (0) 2023.10.30
5972. 택배배송(Java)  (1) 2023.10.23
1446. 지름길 (Java)  (1) 2023.10.21
1916. 최소비용구하기 (Java)  (1) 2023.10.20
'Algorithm & Data Structures/BOJ' 카테고리의 다른 글
  • 1504. 특정한 최단경로 (Java)
  • 2294. 동전2 (Java)
  • 5972. 택배배송(Java)
  • 1446. 지름길 (Java)
Geisha
Geisha
개발 일기
  • Geisha
    Geisha
    Geisha
  • 전체
    오늘
    어제
    • 분류 전체보기 (311) N
      • Algorithm & Data Structures (235) N
        • BOJ (93) N
        • SWEA (1)
        • Programers (137) N
        • Data Structures (3)
      • DB (23) N
        • SQL (17) N
        • RDBMS (2)
      • Java (1)
        • Class (1)
      • Spring (5)
        • Spring MVC (1)
        • Annotations (1)
      • CS (36)
        • 운영체제 (13)
        • 네트워크 (5)
      • Tool (6)
        • Git (5)
        • AWS (1)
      • Project (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    PriorityQueue
    BFS
    스택
    경로압축
    dfs
    백준
    Dijkstra
    프로그래머스
    전위순회
    구현
    골드
    programmers
    알고리즘
    백트래킹
    투포인터
    Stack
    유니온파인드
    algorithm
    동적계획법
    Java
    후위순회
    DynamicProgramming
    Union-Find
    dp
    SQL
    다익스트라
    baekjoon
    이분탐색
    unionfind
    binarySearch
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Geisha
1238. 파티 (Java)
상단으로

티스토리툴바