結果

問題 No.614 壊れたキャンパス
ユーザー 37zigen37zigen
提出日時 2020-04-08 16:57:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,317 ms / 2,000 ms
コード長 4,290 bytes
コンパイル時間 5,265 ms
コンパイル使用メモリ 78,984 KB
実行使用メモリ 151,768 KB
最終ジャッジ日時 2023-09-25 20:05:21
合計ジャッジ時間 20,471 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
50,644 KB
testcase_01 AC 67 ms
51,124 KB
testcase_02 AC 64 ms
50,668 KB
testcase_03 AC 63 ms
50,976 KB
testcase_04 AC 64 ms
50,668 KB
testcase_05 AC 62 ms
50,872 KB
testcase_06 AC 64 ms
50,728 KB
testcase_07 AC 63 ms
51,092 KB
testcase_08 AC 1,165 ms
91,792 KB
testcase_09 AC 1,050 ms
93,468 KB
testcase_10 AC 1,164 ms
122,280 KB
testcase_11 AC 1,244 ms
97,148 KB
testcase_12 AC 1,225 ms
93,504 KB
testcase_13 AC 1,317 ms
95,352 KB
testcase_14 AC 1,267 ms
94,412 KB
testcase_15 AC 1,017 ms
109,292 KB
testcase_16 AC 1,020 ms
109,792 KB
testcase_17 AC 778 ms
151,768 KB
testcase_18 AC 570 ms
86,224 KB
testcase_19 AC 572 ms
87,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		new Main().run();
	}
	
	void updown(long[] dist, int[] level) {
		int N=dist.length;
		for (int i=1;i<N;++i) {
			dist[i]=Math.min(dist[i], dist[i-1]+level[i]-level[i-1]);
		}
		for (int i=N-2;i>=0;--i) {
			dist[i]=Math.min(dist[i], dist[i+1]+level[i+1]-level[i]);
		}
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		
		int N=sc.nextInt();
		int M=sc.nextInt();
		int K=sc.nextInt()-1;
		int S=sc.nextInt()-1;
		int T=sc.nextInt()-1;
		int[][] es=new int[M][3];
		int[][] level=new int[N][];
		long[][] dist=new long[N][];
		long INF=Long.MAX_VALUE/3;
		
		for (int i=0;i<M;++i) {
			es[i][0]=sc.nextInt();// a => a+1
			es[i][1]=sc.nextInt();
			es[i][2]=sc.nextInt();
			--es[i][0];
			--es[i][1];
			--es[i][2];
		}
		
		Arrays.sort(es, Comparator.comparing((int[] v)->v[0]).thenComparing(v->v[0]));
		
		Set<Integer>[] set=new HashSet[N];	
		for (int i=0;i<N;++i) set[i]=new HashSet<>();
		for (int i=0;i<M;++i) {
			set[es[i][0]].add(es[i][1]);
			set[es[i][0]+1].add(es[i][2]);
		}
		set[0].add(S);
		set[N-1].add(T);
		for (int i=0;i<N;++i) { 
			level[i]=new int[set[i].size()];
			dist[i]=new long[set[i].size()];
			Arrays.fill(dist[i], INF);
		}
		for (int i=0;i<N;++i) {
			int p=0;
			for (int v:set[i]) {
				level[i][p++]=v;
			}
			Arrays.sort(level[i]);
		}
		dist[0][Arrays.binarySearch(level[0], S)]=0;
		for (int i=0;i<M;++i) {
			updown(dist[es[i][0]],level[es[i][0]]);
			int j=i;
			while (j+1<M && es[i][0]==es[j+1][0]) ++j;
			for (int k=i;k<=j;++k) {
				int from=Arrays.binarySearch(level[es[k][0]], es[k][1]);
				int to=Arrays.binarySearch(level[es[k][0]+1], es[k][2]);
				dist[es[k][0]+1][to]=Math.min(dist[es[k][0]+1][to], dist[es[k][0]][from]);
			}
			i=j;
		}
		updown(dist[N-1],level[N-1]);

		int goal=Arrays.binarySearch(level[N-1], T);
		System.out.println(dist[N-1][goal]==INF?-1:dist[N-1][goal]);
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    
    public double nextDouble() { return Double.parseDouble(next());}
}
0