2157번: 여행
첫째 줄에 N(1 ≤ N ≤ 300), M(2 ≤ M ≤ N), K(1 ≤ K ≤ 100,000)가 주어진다. K는 개설된 항공로의 개수이다. 다음 K개의 줄에는 각 항공로에 대한 정보를 나타내는 세 정수 a, b, c(1 ≤ a, b ≤ N, 1 ≤ c ≤ 1
www.acmicpc.net
간단한거였는데, 오래걸렸다.
import java.util.*;
public class Main{
public static void main(String[] args) {
int N,M,K;
int[][] dp;
int answer=0;
PriorityQueue<Point> q=new PriorityQueue<>(new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
// TODO Auto-generated method stub
return o1.x-o2.x;
}
});
Scanner sc=new Scanner(System.in);
N=sc.nextInt();
M=sc.nextInt();
K=sc.nextInt();
dp=new int[N+1][M+1];
for(int i=1;i<=K;i++) {
int x=sc.nextInt();
int y=sc.nextInt();
int point=sc.nextInt();
if(x==1) dp[y][1]=Math.max(dp[y][1],point); //1에서 출발하는 경우
else q.add(new Point(x,y,point));
}
while(!q.isEmpty()) {
Point p=q.poll();
for(int i=1;i<M-1;i++) {
if(dp[p.x][i]!=0) dp[p.y][i+1]=Math.max(dp[p.y][i+1],dp[p.x][i]+p.point);
else continue;
}
}
for(int i=1;i<M;i++) {
answer=Math.max(answer, dp[N][i]);
}
System.out.println(answer);
}
static class Point{
int x;
int y;
int point;
public Point(int x, int y, int point) {
this.x=x;
this.y=y;
this.point=point;
}
}
}
'코테 공부' 카테고리의 다른 글
[DP]점프(백준 1890번), 자바(ㅁㅁ) (0) | 2022.06.28 |
---|---|
[DP]숨바꼭질(백준 1697번), 자바(★☆) (0) | 2022.06.27 |
[DP]문제#12.동전1 (0) | 2022.05.11 |
[DP]문제#11.동전2(☆) (0) | 2022.05.11 |
[DP]문제#10. 1로 만들기 (0) | 2022.05.11 |