提出 #63278866


ソースコード 拡げる

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

class Main {
    static ArrayList<Edge>[] outG;
    static ArrayList<Edge>[] inG;
    static int N;
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        
        N = sc.nextInt();
        int M = sc.nextInt();
        int X = sc.nextInt();

        inG = new ArrayList[2*N+1];
        outG = new ArrayList[2*N+1];
        for (int i = 0; i <= 2*N; i++) {
            inG[i] = new ArrayList<>();
            outG[i] = new ArrayList<>();
        }

        for (int i = 0; i < M; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();

            outG[a].add(new Edge(b,1));
            outG[a].add(new Edge(N+a,X));
            outG[N + a].add(new Edge(a,X));

            outG[b].add(new Edge(N+b,X));
            outG[b+N].add(new Edge(b,X));
            outG[N + b].add(new Edge(N+a,1));
        }

        dijkstra(1);
        System.out.println(Math.min(dist[N],dist[2*N]));

        MyWriter.flush();
    }

    static long[] dist;
    //ダイクストラ法
    private static void dijkstra(int s){
        final long INF = 9000000000000000000L;
        boolean[] kakutei = new boolean[2*N + 1]; // Java では new で初期化した配列の要素は false になることに注意
        dist = new long[2*N + 1];
        Arrays.fill(dist, INF);
        // スタート地点をキューに追加
        dist[s] = 0;
        Queue<State> Q = new PriorityQueue<>();
        Q.add(new State(dist[s], 1));

        // ダイクストラ法
        while (Q.size() >= 1) {
            // 次に確定させるべき頂点を求める
            // (ここでは、優先度付きキュー Q の最小要素を取り出し、これを Q から削除する)
            int pos = Q.remove().pos;

            // Q の最小要素が「既に確定した頂点」の場合
            if (kakutei[pos]) {
                continue;
            }

            // cur[x] の値を更新する
            kakutei[pos] = true;
            for (int i = 0; i < outG[pos].size(); i++) {
                Edge e = outG[pos].get(i);
                if (dist[e.to] > dist[pos] + e.cost) {
                    dist[e.to] = dist[pos] + e.cost;
                    Q.add(new State(dist[e.to], e.to));
                }
            }
        }
        //たどりつけない辺を-1に
        for (int i = 1; i <= N; i++) {
            if (dist[i] == INF){
                dist[i] = -1;
            }
        }
    }

    static class State implements Comparable<State> {
        int pos;
        long dist;
        public State(long dist, int pos) {
            this.dist = dist;
            this.pos = pos;
        }
        @Override public int compareTo(State s) {
            // State 型同士の比較をする関数
            if (this.dist < s.dist) {
                return -1;
            }
            if (this.dist > s.dist) {
                return 1;
            }
            return 0;
        }
    }

    static class Edge {
        int to, cost; // 行き先 to、長さ cost
        public Edge(int to, int cost) {
            this.to = to;
            this.cost = cost;
        }
    }

    private static class MyWriter{
        static StringBuilder sb = new StringBuilder();
        protected static void flush(){
            System.out.print(sb);
            sb = new StringBuilder();
        }
        protected static void println(){
            sb.append("\n");
        }
        protected static void println(int i){
            sb.append(i).append("\n");
        }
        protected static void println(long l) {
            sb.append(l).append("\n");
        }
        protected static void println(double d) {
            sb.append(d).append("\n");
        }
        protected static void println(float f) {
            sb.append(f).append("\n");
        }
        protected static void println(char c) {
            sb.append(c).append("\n");
        }
        protected static void println(boolean b) {
            sb.append(b).append("\n");
        }
        protected static void println(Object obj) {
            String s = String.valueOf(obj);
            sb.append(s).append("\n");
        }
        protected static void print(int i){
            sb.append(i);
        }
        protected static void print(long l) {
            sb.append(l);
        }
        protected static void print(double d) {
            sb.append(d);
        }
        protected static void print(float f) {
            sb.append(f);
        }
        protected static void print(char c) {
            sb.append(c);
        }
        protected static void print(boolean b) {
            sb.append(b);
        }
        protected static void print(Object obj) {
            String s = String.valueOf(obj);
            sb.append(s);
        }

        protected static void printInv(long a,long b,long MOD){
            a %= MOD;
            b %= MOD;
            println((a * invGcd(b,MOD)[1])%MOD);
        }
        private static long safeMod(long x, long m){
            x %= m;
            if(x<0) x += m;
            return x;
        }
        private static long[] invGcd(long a, long b){
            a = safeMod(a, b);
            if(a==0) return new long[]{b,0};

            long s=b, t=a;
            long m0=0, m1=1;
            while(t>0){
                long u = s/t;
                s -= t*u;
                m0 -= m1*u;
                long tmp = s; s = t; t = tmp;
                tmp = m0; m0 = m1; m1 = tmp;
            }
            if(m0<0) m0 += b/s;
            return new long[]{s,m0};
        }
        public static long writerGcd(long... a){
            if(a.length == 0) return 0;
            long r = Math.abs(a[0]);
            for(int i=1; i<a.length; i++){
                if(a[i]!=0) {
                    if(r==0) r = Math.abs(a[i]);
                    else r = invGcd(r, Math.abs(a[i]))[0];
                }
            }
            return r;
        }
    }

    private static 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();
                }
                return buflen > 0;
            }
        }

        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());
        }
    }
}

提出情報

提出日時
問題 E - Flip Edge
ユーザ AAH_tomato
言語 Java (OpenJDK 17)
得点 425
コード長 8746 Byte
結果 AC
実行時間 1004 ms
メモリ 154808 KiB

コンパイルエラー

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 425 / 425
結果
AC × 4
AC × 70
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 01_random_64.txt, 01_random_65.txt, 01_random_66.txt, 01_random_67.txt, 01_random_68.txt, 01_random_69.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 43 ms 34444 KiB
00_sample_01.txt AC 43 ms 34316 KiB
00_sample_02.txt AC 44 ms 34304 KiB
00_sample_03.txt AC 42 ms 34260 KiB
01_random_04.txt AC 945 ms 154496 KiB
01_random_05.txt AC 797 ms 145940 KiB
01_random_06.txt AC 842 ms 148320 KiB
01_random_07.txt AC 977 ms 154436 KiB
01_random_08.txt AC 889 ms 153760 KiB
01_random_09.txt AC 857 ms 148088 KiB
01_random_10.txt AC 920 ms 154684 KiB
01_random_11.txt AC 767 ms 146028 KiB
01_random_12.txt AC 940 ms 153872 KiB
01_random_13.txt AC 825 ms 146484 KiB
01_random_14.txt AC 773 ms 146108 KiB
01_random_15.txt AC 881 ms 147888 KiB
01_random_16.txt AC 957 ms 154540 KiB
01_random_17.txt AC 785 ms 145908 KiB
01_random_18.txt AC 866 ms 147804 KiB
01_random_19.txt AC 911 ms 146648 KiB
01_random_20.txt AC 795 ms 146076 KiB
01_random_21.txt AC 881 ms 147856 KiB
01_random_22.txt AC 991 ms 154452 KiB
01_random_23.txt AC 805 ms 146124 KiB
01_random_24.txt AC 973 ms 153592 KiB
01_random_25.txt AC 955 ms 154808 KiB
01_random_26.txt AC 935 ms 153852 KiB
01_random_27.txt AC 983 ms 153832 KiB
01_random_28.txt AC 887 ms 146632 KiB
01_random_29.txt AC 820 ms 145932 KiB
01_random_30.txt AC 1004 ms 153616 KiB
01_random_31.txt AC 703 ms 107932 KiB
01_random_32.txt AC 331 ms 72816 KiB
01_random_33.txt AC 434 ms 91800 KiB
01_random_34.txt AC 724 ms 115888 KiB
01_random_35.txt AC 473 ms 92620 KiB
01_random_36.txt AC 492 ms 91664 KiB
01_random_37.txt AC 527 ms 91180 KiB
01_random_38.txt AC 325 ms 91276 KiB
01_random_39.txt AC 471 ms 91288 KiB
01_random_40.txt AC 629 ms 107728 KiB
01_random_41.txt AC 537 ms 107796 KiB
01_random_42.txt AC 173 ms 48968 KiB
01_random_43.txt AC 642 ms 107884 KiB
01_random_44.txt AC 386 ms 90660 KiB
01_random_45.txt AC 847 ms 145320 KiB
01_random_46.txt AC 873 ms 147304 KiB
01_random_47.txt AC 795 ms 143392 KiB
01_random_48.txt AC 874 ms 147684 KiB
01_random_49.txt AC 783 ms 143296 KiB
01_random_50.txt AC 807 ms 147672 KiB
01_random_51.txt AC 759 ms 142944 KiB
01_random_52.txt AC 827 ms 147340 KiB
01_random_53.txt AC 752 ms 143024 KiB
01_random_54.txt AC 822 ms 147796 KiB
01_random_55.txt AC 765 ms 143080 KiB
01_random_56.txt AC 762 ms 147168 KiB
01_random_57.txt AC 779 ms 147408 KiB
01_random_58.txt AC 767 ms 147332 KiB
01_random_59.txt AC 838 ms 147200 KiB
01_random_60.txt AC 858 ms 146952 KiB
01_random_61.txt AC 867 ms 147380 KiB
01_random_62.txt AC 632 ms 107844 KiB
01_random_63.txt AC 639 ms 107668 KiB
01_random_64.txt AC 616 ms 107352 KiB
01_random_65.txt AC 43 ms 34276 KiB
01_random_66.txt AC 42 ms 34512 KiB
01_random_67.txt AC 42 ms 34260 KiB
01_random_68.txt AC 110 ms 68268 KiB
01_random_69.txt AC 85 ms 46552 KiB