MOVE 문제에서 RTE가 나오는 이유를 모르겠습니다.

  • gunofeban
    gunofeban

    MOVE

    계속 시도하고 있는데 RTE 가 외 나오는지 잘 모르겠습니다..ㅠㅠ

    혹시 어떤 점에 문제가 있는지 알려주실 수 있나요 ? ㅠㅠ

    부탁드립니다

    아래는 제 코드 내용입니다

    import java.io.IOException;
    import java.util.PriorityQueue;
    import java.util.Scanner;
    import java.util.Comparator;

    public class Main {
    static int[][] map;
    static int[] distance;
    static int node,edge,start,end;

    static PriorityQueue<Element> pq;
    static int inf = 99999999;
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int tc = scanner.nextInt();
    
        for(int i=0;i<tc;i++){
            pq = new PriorityQueue<Element>(10000,new Comparator<Element>() {
    
                @Override
                public int compare(Element o1, Element o2) {
                    // TODO Auto-generated method stub
                    if(o1.getDistance() <= o2.getDistance())
                        return -1;
                    else if (o1.getDistance() > o2.getDistance())
                        return 1;
    
                    return 0;
                }
            });
    
            node = scanner.nextInt();
            edge = scanner.nextInt();
    
            map = new int[node][node];
            distance = new int[node];
    
            for(int j=0;j<node;j++){
                for(int k=0;k<node;k++){
                    map[j][k] = -1;
                }
            }
            for(int j=0;j<edge;j++){
                map[scanner.nextInt()][scanner.nextInt()] = scanner.nextInt();
            }
            for(int j=0;j<node;j++){
                distance[j]=inf;
            }
    
    
            start = 0;
            end = node-1;
    
            distance[start]=0;
            pq.offer(new Element(start,distance[start]));
    
            while(!pq.isEmpty()){
                int cost = pq.peek().getDistance();
                int here = pq.peek().getIndex();
                pq.poll();
    
                if(cost > distance[here])
                    continue;
    
                for(int j=0;j<node;j++){
                    if(map[here][j] != -1 && distance[j] > distance[here]+ map[here][j]){
                        distance[j] = distance[here] + map[here][j];
                        pq.offer(new Element(j,distance[j]));
                    }
                }
            }
    
            System.out.println(distance[end]);
        }
        scanner.close();
    }
    static class Element{
    
        int index;
        int distance;
    
        public Element(int index, int distance){
            this.index = index;
            this.distance = distance;
        }
        public int getIndex(){
            return index;
        }
        public int getDistance(){
            return distance;
        }
    
    }

    }


    7년 전
1개의 댓글이 있습니다.
  • Corea
    Corea

    map 배열을 N * N 크기로 잡으셨는데, 우선 이 부분에서 메모리 할당이 불가능할 것 같습니다. 문제의 메모리 제한은 64M인데, N이 10000인 경우 300M 이상의 메모리를 할당하게됩니다.


    7년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.