Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 2020 KAKAO BLIND
- 포두부 보쌈
- 알고리즘
- 쓰레드 풀
- 스프링
- 동적 프로그래밍
- 다이나믹 프로그래밍
- 양꼬치
- 설탕 배달
- 호유동
- 완도산회
- 프로그래머스
- BFS
- Servlet
- 스프링 MVC
- 2589
- mvc
- 백준
- 2839
- 맛집 투어
- 투어
- Spring
- HTTP API
- dp
- 1로 만들기
- 맛집
- 2638
- 고모네 콩탕
- 서블릿
- 문자열 압축
Archives
- Today
- Total
프로그래밍 공방
[백준] 1238번 : 파티 본문
문제
1238번: 파티
첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), X가 공백으로 구분되어 입력된다. 두 번째 줄부터 M+1번째 줄까지 i번째 도로의 시작점, 끝점, 그리고 이 도로를 지나는데 필요한 소요시간 Ti가 들어
www.acmicpc.net
문제해결방법
처음에는 다른 모든 학생들한테서 X까지의 거리를 알아내기 위해 다익스트라를 해야되는지, 플로이드 워셜을 해야되는지 고민했었다.
그런데 생각해보니까 도로를 뒤집어서 그 뒤집은 도로로 X에서 출발하는 다익스트라를 돌리면 각 학생들이 X로 오는 거리를 구할 수 있다는 걸 알아내서 그 방법으로 풀었다.
코드
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | package baekjoon; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main1238_파티 { public static int N, M, X; public static int[] dist, reverseDist; public static List<int[]>[] edge, reverseEdge; public static PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() { @Override public int compare(int[] arg0, int[] arg1) { return Integer.compare(arg0[1], arg1[1]); } }); public static void main(String[] args) throws Exception { 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()); dist = new int[N+1]; reverseDist = new int[N+1]; edge = new List[N+1]; reverseEdge = new List[N+1]; for(int i=1; i<=N; i++) { edge[i] = new ArrayList<>(); reverseEdge[i] = new ArrayList<>(); if(i==X) continue; dist[i] = Integer.MAX_VALUE; reverseDist[i] = Integer.MAX_VALUE; } 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()); edge[s].add(new int[] {e, v}); reverseEdge[e].add(new int[] {s, v}); } pq.add(new int[] {X, 0}); while(!pq.isEmpty()) { int[] cur = pq.poll(); for(int i=0; i<edge[cur[0]].size(); i++) { int[] next = edge[cur[0]].get(i); if(dist[next[0]]>cur[1]+next[1]) { dist[next[0]]=cur[1]+next[1]; pq.add(new int[] {next[0], dist[next[0]]}); } } } pq.add(new int[] {X, 0}); while(!pq.isEmpty()) { int[] cur = pq.poll(); for(int i=0; i<reverseEdge[cur[0]].size(); i++) { int[] next = reverseEdge[cur[0]].get(i); if(reverseDist[next[0]]>cur[1]+next[1]) { reverseDist[next[0]]=cur[1]+next[1]; pq.add(new int[] {next[0], reverseDist[next[0]]}); } } } int max = Integer.MIN_VALUE; for(int i=1; i<=N; i++) { if(i==X) continue; if(max<dist[i]+reverseDist[i]) max=dist[i]+reverseDist[i]; } System.out.println(max); } } | cs |
코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.
'개발 > 문제해결' 카테고리의 다른 글
[프로그래머스] 외벽 점검 (0) | 2021.01.29 |
---|---|
[프로그래머스] 광고 삽입 (5) | 2021.01.27 |
[프로그래머스] 신규 아이디 추천 (0) | 2021.01.27 |
[프로그래머스] 합승 택시 요금 (0) | 2021.01.26 |
[백준] 1010번 : 다리놓기 (0) | 2021.01.26 |