結果

問題 No.2565 はじめてのおつかい
ユーザー ゆうきゆうき
提出日時 2023-12-02 15:47:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 536 ms / 2,000 ms
コード長 12,174 bytes
コンパイル時間 4,122 ms
コンパイル使用メモリ 94,580 KB
実行使用メモリ 91,960 KB
最終ジャッジ日時 2023-12-02 15:47:40
合計ジャッジ時間 22,716 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
52,436 KB
testcase_01 AC 59 ms
54,384 KB
testcase_02 AC 67 ms
54,404 KB
testcase_03 AC 536 ms
89,404 KB
testcase_04 AC 437 ms
91,960 KB
testcase_05 AC 59 ms
54,264 KB
testcase_06 AC 331 ms
69,576 KB
testcase_07 AC 195 ms
62,456 KB
testcase_08 AC 222 ms
63,632 KB
testcase_09 AC 286 ms
67,896 KB
testcase_10 AC 253 ms
68,024 KB
testcase_11 AC 472 ms
81,000 KB
testcase_12 AC 153 ms
62,108 KB
testcase_13 AC 272 ms
63,980 KB
testcase_14 AC 465 ms
71,256 KB
testcase_15 AC 356 ms
70,508 KB
testcase_16 AC 351 ms
81,984 KB
testcase_17 AC 143 ms
61,696 KB
testcase_18 AC 205 ms
66,192 KB
testcase_19 AC 492 ms
78,880 KB
testcase_20 AC 418 ms
78,968 KB
testcase_21 AC 385 ms
78,880 KB
testcase_22 AC 259 ms
67,424 KB
testcase_23 AC 318 ms
69,156 KB
testcase_24 AC 138 ms
59,124 KB
testcase_25 AC 431 ms
78,852 KB
testcase_26 AC 262 ms
66,408 KB
testcase_27 AC 341 ms
79,168 KB
testcase_28 AC 428 ms
79,136 KB
testcase_29 AC 438 ms
83,404 KB
testcase_30 AC 421 ms
80,868 KB
testcase_31 AC 440 ms
78,872 KB
testcase_32 AC 284 ms
68,196 KB
testcase_33 AC 413 ms
78,916 KB
testcase_34 AC 393 ms
74,504 KB
testcase_35 AC 373 ms
82,864 KB
testcase_36 AC 487 ms
78,952 KB
testcase_37 AC 272 ms
68,540 KB
testcase_38 AC 395 ms
72,064 KB
testcase_39 AC 392 ms
71,196 KB
testcase_40 AC 444 ms
83,016 KB
testcase_41 AC 389 ms
82,988 KB
testcase_42 AC 236 ms
67,848 KB
testcase_43 AC 391 ms
74,452 KB
testcase_44 AC 295 ms
65,960 KB
testcase_45 AC 180 ms
61,984 KB
testcase_46 AC 338 ms
69,580 KB
testcase_47 AC 230 ms
66,792 KB
testcase_48 AC 281 ms
67,912 KB
testcase_49 AC 155 ms
62,268 KB
testcase_50 AC 207 ms
66,664 KB
testcase_51 AC 60 ms
52,160 KB
testcase_52 AC 134 ms
63,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import static java.lang.Math.*;
import static java.util.Arrays.*;

import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.*;
import java.util.stream.*;

class Solver{
  long st = System.currentTimeMillis();

  long elapsed(){ return System.currentTimeMillis() -st; }

  void reset(){ st = System.currentTimeMillis(); }

  static int infI = (1 <<30) -1;
  static long infL = 1L <<60;
  //static long mod = (int) 1e9 +7;
  static long mod = 998244353;
  static String yes = "Yes";
  static String no = "No";

  static Random rd = ThreadLocalRandom.current();
  MyReader in = new MyReader(System.in);
  MyWriter out = new MyWriter(System.out);
  MyWriter log = new MyWriter(System.err){
    @Override
    void println(Object obj){ super.println(obj == null ? "null" : obj); };

    @Override
    protected void ln(){
      super.ln();
      flush();
    };
  };

  Object solve(){
    int N = in.it();
    int M = in.it();
    var g = new Dijkstra<Integer,Integer>(N,true){
     protected Integer zero(){return 0;}
     protected Integer inf(){return infI;}
     protected Integer f(Integer l,Integer val){return l+val;}
    };
    while(M-->0){
      int u = in.idx();
      int v = in.idx();
      g.addEdge(u,v,1);
    }
    
    long ans = infL;
    {
      long tmp = 0;
      g.calc(0,N-2);
      tmp+=g.len(N-2);
      g.calc(N-2,N-1);
      tmp+=g.len(N-1);
      g.calc(N-1,0);
      tmp+=g.len(0);
      ans = min(ans,tmp);
    }
    {
      long tmp = 0;
      g.calc(0,N-1);
      tmp+=g.len(N-1);
      g.calc(N-1,N-2);
      tmp+=g.len(N-2);
      g.calc(N-2,0);
      tmp+=g.len(0);
      ans = min(ans,tmp);
    }
    
    return ans >= infI?-1:ans;
  }
  long ceil(long a,long b){return (a+b-1)/b;}
}

class DeletablePque<T> {
  private Queue<T> que,rem;
  private Comparator<T> cmp;

  @SuppressWarnings("unchecked")
  public DeletablePque(){ this((Comparator<T>) Comparator.naturalOrder()); }

  public <U extends Comparable<U>> DeletablePque(Function<T, U> func){ this(Comparator.comparing(func)); }

  private DeletablePque(Comparator<T> cmp){
    this.cmp = cmp;
    que = new PriorityQueue<>(cmp);
    rem = new PriorityQueue<>(cmp);
  }

  public boolean add(T t){ return que.add(t); }

  public boolean remove(T t){ return rem.add(t); }

  public T poll(){ return adj().poll(); }

  public T peek(){ return adj().peek(); }

  private Queue<T> adj(){
    while (!que.isEmpty() && !rem.isEmpty()
        && cmp.compare(que.peek(),rem.peek()) == 0) {
      que.poll();
      rem.poll();
    }
    return que;
  }
}

class Mex{
  private int[] cnt;
  private DeletablePque<Integer> que;
  private Queue<Integer> wait;
  private int add;

  public Mex(){ this(16); }

  public Mex(int n){
    cnt = new int[n];
    que = new DeletablePque<>();
    wait = new PriorityQueue<>();
    for (int x = 0;x < cnt.length;x++)
      que.add(x);
  }

  public int add(int x){
    if (++add == cnt.length)
      grow();
    if (x < cnt.length) {
      if (cnt[x]++ == 0)
        que.remove(x);
    } else
      wait.add(x);
    return que.peek();
  }

  public int remove(int x){
    if (x < cnt.length && --cnt[x] == 0)
      que.add(x);
    return que.peek();
  }

  private void grow(){
    for (int x = cnt.length;x < cnt.length <<1;x++)
      que.add(x);
    for (cnt = copyOf(cnt,cnt.length <<1);!wait.isEmpty() && wait.peek() < cnt.length;add--)
      add(wait.poll());
  }

}

abstract class Dijkstra<E, L> extends Graph<E>{
  protected L[] len;
  private int[] arr,rev;
  private int sz;
  protected Edge<E>[] pre;
  private Comparator<L> cmp;
  protected L zero,inf;

  @SuppressWarnings("unchecked")
  public Dijkstra(int n,boolean dir){ this(n,dir,(Comparator<L>) Comparator.naturalOrder()); }

  public <U extends Comparable<U>> Dijkstra(int n,boolean dir,Function<L, U> func){
    this(n,dir,Comparator.comparing(func));
  }

  @SuppressWarnings("unchecked")
  public Dijkstra(int n,boolean dir,Comparator<L> cmp){
    super(n,dir);
    this.cmp = cmp;
    zero = zero();
    inf = inf();
    len = (L[]) new Object[n];
    arr = new int[n];
    rev = new int[n];
    pre = new Edge[n];
  }

  protected abstract L zero();

  protected abstract L inf();

  protected abstract L f(L l,E val);

  public void calcFrom(int s){ calc(s,-1); }

  public void calc(int s,int g){
    init();
    pre[s] = null;
    set(s,zero);
    while (!isEmpty()) {
      var cur = poll();
      if (cur == g)
        break;
      L l = len[cur];
      for (var e:go(cur)) {
        L ll = f(l,e.val);
        if (cmp.compare(ll,len[e.v]) < 0) {
          pre[e.v] = e;
          set(e.v,ll);
        }
      }
    }
  }

  public L len(int t){ return len[t]; }

  public Deque<Edge<E>> path(int t){
    Deque<Edge<E>> ret = new ArrayDeque<>();
    while (pre[t] != null) {
      ret.addFirst(pre[t]);
      t = pre[t].u;
    }

    return ret;
  }

  private void init(){
    fill(len,inf);
    setAll(arr,i -> i);
    setAll(rev,i -> i);
    sz = n;
  }

  private boolean isEmpty(){ return sz == 0; }

  private void set(int i,L l){
    if (sz <= rev[i] || cmp.compare(len[i],l) <= 0)
      return;
    len[i] = l;
    heapfy(rev[i]);
  }

  private int poll(){
    int ret = arr[0];
    heapfy(swap(0,--sz));
    return ret;
  }

  private void heapfy(int k){
    int p = k -1 >>1;
    if (0 <= p && cmp.compare(len[arr[p]],len[arr[k]]) > 0) {
      heapfy(swap(p,k));
      return;
    }

    int c = k <<1 |1;
    if (sz <= c)
      return;

    if (c +1 < sz && cmp.compare(len[arr[c +1]],len[arr[c]]) < 0)
      c++;

    if (cmp.compare(len[arr[c]],len[arr[k]]) < 0)
      heapfy(swap(c,k));
  }

  private int swap(int i,int j){
    int t = arr[i];
    arr[i] = arr[j];
    arr[j] = t;
    rev[arr[i]] = i;
    rev[arr[j]] = j;
    return i;
  }
}

class Edge<L> {
  int id,u,v;
  L val;
  Edge<L> re;

  Edge(int id,int u,int v,L val){
    this.id = id;
    this.u = u;
    this.v = v;
    this.val = val;
  }

  @Override
  public String toString(){ return "(" +u +"," +v +"," +val +")"; }
}

class UnionFind{
  int num;
  int[] dat;
  int[] nxt;

  public UnionFind(int n){
    dat = new int[n];
    nxt = new int[n];
    setAll(nxt,i -> i);
    fill(dat,-1);
    num = n;
  }

  int root(int x){ return dat[x] < 0 ? x : (dat[x] = root(dat[x])); }

  boolean same(int u,int v){ return root(u) == root(v); }

  boolean unite(int u,int v){
    if ((u = root(u)) == (v = root(v)))
      return false;

    if (dat[u] > dat[v]) {
      u ^= v;
      v ^= u;
      u ^= v;
    }
    dat[u] += dat[v];
    dat[v] = u;
    num--;
    nxt[u] ^= nxt[v];
    nxt[v] ^= nxt[u];
    nxt[u] ^= nxt[v];
    return true;
  }

  int size(int x){ return -dat[root(x)]; }

  int[] getGroup(int x){
    int[] ret = new int[size(x)];
    for (int i = 0,c = root(x);i < ret.length;i++)
      ret[i] = c = nxt[c];
    return ret;
  }

}

class Graph<L> {
  public int n;
  List<Edge<L>> es;
  private List<Edge<L>>[] go,bk;

  @SuppressWarnings("unchecked")
  public Graph(int n,boolean dir){
    this.n = n;
    go = new List[n];
    bk = dir ? new List[n] : go;
    for (int i = 0;i < n;i++) {
      go[i] = new ArrayList<>();
      bk[i] = new ArrayList<>();
    }
    es = new ArrayList<>();
  }

  public void addEdge(int u,int v){ addEdge(u,v,null); }

  public void addEdge(int u,int v,L l){
    var e = new Edge<>(es.size(),u,v,l);
    var re = new Edge<>(e.id,e.v,e.u,inv(e.val));
    es.add(e);
    go[u].add(re.re = e);
    bk[v].add(e.re = re);
  }

  protected L inv(L l){ return l; }

  public List<Edge<L>> go(int ui){ return go[ui]; }

  public List<Edge<L>> back(int ui){ return bk[ui]; }
}

class Util{
  static int[] arrI(int N,IntUnaryOperator f){
    int[] ret = new int[N];
    setAll(ret,f);
    return ret;
  }

  static long[] arrL(int N,IntToLongFunction f){
    long[] ret = new long[N];
    setAll(ret,f);
    return ret;
  }

  static double[] arrD(int N,IntToDoubleFunction f){
    double[] ret = new double[N];
    setAll(ret,f);
    return ret;
  }

  static <T> T[] arr(T[] arr,IntFunction<T> f){
    setAll(arr,f);
    return arr;
  }
}

class MyReader{
  private byte[] buf = new byte[1 <<16];
  private int ptr,tail;
  private InputStream in;

  MyReader(InputStream in){ this.in = in; }

  private byte read(){
    if (ptr == tail)
      try {
        tail = in.read(buf);
        ptr = 0;
      } catch (IOException e) {}
    return buf[ptr++];
  }

  private boolean isPrintable(byte c){ return 32 < c && c < 127; }

  private byte nextPrintable(){
    byte ret = read();
    while (!isPrintable(ret))
      ret = read();
    return ret;
  }

  int it(){ return toIntExact(lg()); }

  int[] it(int N){ return Util.arrI(N,i -> it()); }

  int[][] it(int H,int W){ return Util.arr(new int[H][],i -> it(W)); }

  int idx(){ return it() -1; }

  int[] idx(int N){ return Util.arrI(N,i -> idx()); }

  int[][] idx(int H,int W){ return Util.arr(new int[H][],i -> idx(W)); }

  long lg(){
    byte i = nextPrintable();
    boolean negative = i == 45;
    long n = negative ? 0 : i -'0';
    while (isPrintable(i = read()))
      n = 10 *n +i -'0';
    return negative ? -n : n;
  }

  long[] lg(int N){ return Util.arrL(N,i -> lg()); }

  long[][] lg(int H,int W){ return Util.arr(new long[H][],i -> lg(W)); }

  double dbl(){ return Double.parseDouble(str()); }

  double[] dbl(int N){ return Util.arrD(N,i -> dbl()); }

  double[][] dbl(int H,int W){ return Util.arr(new double[H][],i -> dbl(W)); }

  char[] ch(){ return str().toCharArray(); }

  char[][] ch(int H){ return Util.arr(new char[H][],i -> ch()); }

  String line(){
    StringBuilder sb = new StringBuilder();
    for (byte c;(c = read()) != '\n';)
      sb.append((char) c);
    return sb.toString();
  }

  String str(){
    StringBuilder sb = new StringBuilder();
    sb.append((char) nextPrintable());
    for (byte c;isPrintable(c = read());)
      sb.append((char) c);
    return sb.toString();
  }

  String[] str(int N){ return Util.arr(new String[N],i -> str()); }
}

class MyWriter{
  private OutputStream out;
  private byte[] buf = new byte[1 <<16],ibuf = new byte[20];
  private int tail;

  MyWriter(OutputStream out){ this.out = out; }

  void flush(){
    try {
      out.write(buf,0,tail);
      tail = 0;
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  protected void ln(){ write((byte) '\n'); }

  private void write(byte b){
    buf[tail++] = b;
    if (tail == buf.length)
      flush();
  }

  private void write(byte[] b,int off,int len){
    for (int i = off;i < off +len;i++)
      write(b[i]);
  }

  private void write(long n){
    if (n < 0) {
      n = -n;
      write((byte) '-');
    }
    int i = ibuf.length;
    do {
      ibuf[--i] = (byte) (n %10 +'0');
      n /= 10;
    } while (n > 0);
    write(ibuf,i,ibuf.length -i);
  }

  private void print(Object obj){
    if (obj instanceof Boolean)
      print((boolean) obj ? Solver.yes : Solver.no);
    else if (obj instanceof Integer)
      write((int) obj);
    else if (obj instanceof Long)
      write((long) obj);
    else if (obj instanceof char[] cs)
      for (char b:cs)
        write((byte) b);
    else if (obj.getClass().isArray()) {
      int l = Array.getLength(obj);
      for (int i = 0;i < l;i++) {
        print(Array.get(obj,i));
        if (i +1 < l)
          write((byte) ' ');
      }
    } else
      print(Objects.toString(obj).toCharArray());
  }

  void println(Object obj){
    if (obj == null)
      return;
    if (obj instanceof Iterable<?> co)
      for (Object e:co)
        println(e);
    else if (obj.getClass().isArray() && Array.getLength(obj) > 0 && Array.get(obj,0).getClass().isArray()) {
      int l = Array.getLength(obj);
      for (int i = 0;i < l;i++)
        println(Array.get(obj,i));
    } else {
      print(obj);
      ln();
    }
  }

  void printlns(Object... o){
    print(o);
    ln();
  }
}

class Main{
  public static void main(String[] args) throws Exception{
    new Solver(){
      public void exe(){
        out.println(solve());
        out.flush();
        log.println(elapsed());
      }
    }.exe();
  }
}
0