Guest User

Untitled

a guest
Oct 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. package study;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class adCar {
  6. static int max,N;
  7. static int[] length;
  8. static int[] time;
  9. static int[] sum;
  10. static boolean[] visited;
  11. public static void main(String[] args) {
  12. Scanner sc=new Scanner(System.in);
  13.  
  14. max=sc.nextInt();
  15. N=sc.nextInt();
  16.  
  17. length=new int[N+1];
  18. visited=new boolean[N+1];
  19. time=new int[N+1];
  20. sum=new int[N+1];
  21.  
  22. int tmp=0;
  23. for(int i=0; i<=N; i++) {
  24. length[i]=sc.nextInt()+tmp;
  25. tmp=length[i];
  26. }
  27. for(int i=1; i<=N; i++) {
  28. time[i]=sc.nextInt();
  29. sum[i]=Integer.MAX_VALUE;
  30. }
  31.  
  32. DFS(0,0);//위치, sum
  33. System.out.println(min);
  34. }
  35. static int min=Integer.MAX_VALUE;
  36. private static void DFS(int v, int sum) {
  37. if(v==N) {
  38. if(min>sum) min=sum;
  39. return;
  40. }
  41. for(int i=0; i<=N; i++) {
  42. if(visited[i]==false) {
  43. if(length[i]-length[v]<=max) {
  44. visited[i]=true;
  45. DFS(i,sum+time[i]);
  46. visited[i]=false;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. //각 구간의 길이를 누적으로 합산해 준다.
  53. //시간 정보는 1부터 N까지 저장.
  54. // 0~N까지 돌면서 현재구간에서 이전 구간을 빼면!!! 그 구간사이의 거리를 알 수 있다. -> 정해진 길이보다 적으면 조건 만족!!!
  55. //조건을 만족하면 visited체크 해주고, 해당 거리의 시간을 sum에 합해 준다.
  56. //종로 지접에 도착 했을때 매번 더 작은 크기로 업데이트 해준다.
  57. }
Add Comment
Please, Sign In to add comment