結果

問題 No.496 ワープクリスタル (給料日前編)
ユーザー yun_appyun_app
提出日時 2017-03-28 17:35:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 3,503 bytes
コンパイル時間 2,446 ms
コンパイル使用メモリ 80,036 KB
実行使用メモリ 57,520 KB
最終ジャッジ日時 2023-09-20 18:33:10
合計ジャッジ時間 7,090 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
51,272 KB
testcase_01 AC 74 ms
50,696 KB
testcase_02 AC 77 ms
52,448 KB
testcase_03 AC 77 ms
52,520 KB
testcase_04 AC 78 ms
52,636 KB
testcase_05 AC 79 ms
52,656 KB
testcase_06 AC 43 ms
49,500 KB
testcase_07 AC 44 ms
49,500 KB
testcase_08 AC 190 ms
56,908 KB
testcase_09 AC 179 ms
57,016 KB
testcase_10 AC 137 ms
54,196 KB
testcase_11 AC 138 ms
54,460 KB
testcase_12 AC 113 ms
54,284 KB
testcase_13 AC 122 ms
54,472 KB
testcase_14 AC 79 ms
52,640 KB
testcase_15 AC 90 ms
52,828 KB
testcase_16 AC 128 ms
53,544 KB
testcase_17 AC 117 ms
53,284 KB
testcase_18 AC 82 ms
52,812 KB
testcase_19 AC 122 ms
53,588 KB
testcase_20 AC 91 ms
52,604 KB
testcase_21 AC 89 ms
52,700 KB
testcase_22 AC 221 ms
57,520 KB
testcase_23 AC 219 ms
57,228 KB
testcase_24 AC 190 ms
56,892 KB
testcase_25 AC 152 ms
56,484 KB
testcase_26 AC 161 ms
57,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] lines = br.readLine().split(" ");
        int gx = Integer.parseInt(lines[0]);
        int gy = Integer.parseInt(lines[1]);
        int n  = Integer.parseInt(lines[2]);
        int f  = Integer.parseInt(lines[3]);
        
        Place start = new Place(0,0);
        Place goal = new Place(gx,gy);
        ArrayList<Crystal> crystals = new ArrayList<Crystal>();
        for(int i=0;i<n;i++){
            crystals.add(new Crystal(i,br.readLine()));
        }
        
        PriorityQueue<Place> queue = new PriorityQueue<Place>(1, new Comparator<Place>(){
            public int compare(Place p1,Place p2){
                if(p1.c!=p2.c){
                    return p1.c-p2.c;
                }
                return p2.x+p2.y - p1.x - p1.y;
            }
        });
        queue.add(start);
        HashMap<String,Integer> routemap = new HashMap<String,Integer>();
        while(!queue.isEmpty()){
            Place now = queue.poll();
            if(now.x == goal.x && now.y == goal.y){
                goal = now;
                break;
            }
            for(Crystal c:crystals){
                if(now.canUseCrystal(c)){
                    Place newPlace = now.clone();
                    newPlace.useCrystal(c);
                    String key = newPlace.x + "_" + newPlace.y;
                    if(newPlace.x <= goal.x && newPlace.y <= goal.y
                    && (!routemap.containsKey(key) || routemap.get(key)>newPlace.c)){
                        queue.add(newPlace);
                        routemap.put(key,newPlace.c);
                    }
                }
            }
            Place newPlace = now.clone();
            newPlace.move(goal,f);
            queue.add(newPlace);
        }
        System.out.println(goal.c);
    }
    private static class Place{
        int x,y;
        int c;
        ArrayList<Integer> useCrystal = new ArrayList<Integer>();
        
        public Place(int x,int y){
            this.x = x;
            this.y = y;
            this.c = 0;
        }
        public Place clone(){
            Place p = new Place(this.x,this.y);
            p.c = this.c;
            for(int n:this.useCrystal){
                p.useCrystal.add(n);
            }
            return p;
        }
        public boolean canUseCrystal(Crystal c){
            return useCrystal.indexOf(c.no)==-1;
        }
        public void useCrystal(Crystal c){
            this.x += c.x;
            this.y += c.y;
            this.c += c.c;
            useCrystal.add(c.no);
        }
        public void move(Place g,int f){
            int cost = f*(g.x-this.x);
            cost += f*(g.y-this.y);
            this.x = g.x;
            this.y = g.y;
            this.c += cost;
        }
    }
    private static class Crystal{
        public int no;
        public int x,y,c;
        public Crystal(int no,String line){
            this.no = no;
            String[] lines = line.split(" ");
            this.x = Integer.parseInt(lines[0]);
            this.y = Integer.parseInt(lines[1]);
            this.c = Integer.parseInt(lines[2]);
        }
    }
}
0