Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package study;
- import java.util.Scanner;
- public class adCar {
- static int max,N;
- static int[] length;
- static int[] time;
- static int[] sum;
- static boolean[] visited;
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- max=sc.nextInt();
- N=sc.nextInt();
- length=new int[N+1];
- visited=new boolean[N+1];
- time=new int[N+1];
- sum=new int[N+1];
- int tmp=0;
- for(int i=0; i<=N; i++) {
- length[i]=sc.nextInt()+tmp;
- tmp=length[i];
- }
- for(int i=1; i<=N; i++) {
- time[i]=sc.nextInt();
- sum[i]=Integer.MAX_VALUE;
- }
- DFS(0,0);//위치, sum
- System.out.println(min);
- }
- static int min=Integer.MAX_VALUE;
- private static void DFS(int v, int sum) {
- if(v==N) {
- if(min>sum) min=sum;
- return;
- }
- for(int i=0; i<=N; i++) {
- if(visited[i]==false) {
- if(length[i]-length[v]<=max) {
- visited[i]=true;
- DFS(i,sum+time[i]);
- visited[i]=false;
- }
- }
- }
- }
- //각 구간의 길이를 누적으로 합산해 준다.
- //시간 정보는 1부터 N까지 저장.
- // 0~N까지 돌면서 현재구간에서 이전 구간을 빼면!!! 그 구간사이의 거리를 알 수 있다. -> 정해진 길이보다 적으면 조건 만족!!!
- //조건을 만족하면 visited체크 해주고, 해당 거리의 시간을 sum에 합해 준다.
- //종로 지접에 도착 했을때 매번 더 작은 크기로 업데이트 해준다.
- }
Add Comment
Please, Sign In to add comment