Submission #1989000


Source Code Expand

import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

public class Main {

  private static void solve() {
    int n = ni();
    int m = ni();
    int[] from = new int[m];
    int[] to = new int[m];
    int[] w = new int[m];
    for (int i = 0; i < m; i++) {
      from[i] = ni() - 1;
      to[i] = ni() - 1;
      w[i] = ni();
    }
    int[][][] g = packWU(n, from, to, w);
    long ret = dijk(g);
    System.out.println(ret);
  }

  public static long dijk(int[][][] g) {

    Map<Long, Long> td = new HashMap<>();
    
    //[v, color, len]
    TreeSet<long[]> q = new TreeSet<>((a, b) -> {
      if (a[2] - b[2] != 0)
        return Long.compare(a[2], b[2]);
      if (a[1] - b[1] != 0)
        return Long.compare(a[1], b[1]);
      return Long.compare(a[0], b[0]);
    });

    long inf = 10000000;
    int target = g.length - 1;
    long[] start = new long[] {0, 0, 0};
    q.add(start);
    td.put(0L, 0L);

    long ret = -1;
    while (q.size() > 0) {
      long[] v = q.pollFirst();
      int cur = (int)v[0];
      int color = (int)v[1];

      for (int i = 0; i < g[cur].length; i++) {
        int nv = g[cur][i][0];
        int nc = g[cur][i][1];

        long[] next = new long[] {nv, nc, v[2]};
        if (color == 0) {
          next[2] ++;
        } else if (color == nc) {
        } else {
          next[0] = cur;
          next[1] = 0;
        }

        long hash = next[1] * inf + next[0];
        if (!td.containsKey(hash) || next[2] < td.get(hash)) {
          q.remove(next);
          td.put(hash, next[2]);
          q.add(next);

          if (next[0] == target) {
            ret = ret < 0 ? next[2] : Math.min(ret, next[2]);
          }
        }
      }
    }

    return ret;
  }


  public static int[][][] packWU(int n, int[] from, int[] to, int[] w) {
    return packWU(n, from, to, w, from.length);
  }

  public static int[][][] packWU(int n, int[] from, int[] to, int[] w, int sup) {
    int[][][] g = new int[n][][];
    int[] p = new int[n];
    for (int i = 0; i < sup; i++)
      p[from[i]]++;
    for (int i = 0; i < sup; i++)
      p[to[i]]++;
    for (int i = 0; i < n; i++)
      g[i] = new int[p[i]][2];
    for (int i = 0; i < sup; i++) {
      --p[from[i]];
      g[from[i]][p[from[i]]][0] = to[i];
      g[from[i]][p[from[i]]][1] = w[i];
      --p[to[i]];
      g[to[i]][p[to[i]]][0] = from[i];
      g[to[i]][p[to[i]]][1] = w[i];
    }
    return g;
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = System.getProperty("debug");
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}

Submission Info

Submission Time
Task E - Snuke's Subway Trip
User hiromi_ayase
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 4959 Byte
Status TLE
Exec Time 3162 ms
Memory 386368 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status AC
AC × 47
TLE × 6
MLE × 6
Set Name Test Cases
Sample
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, sample_01.txt, sample_02.txt, sample_03.txt, w1.txt, w10.txt, w11.txt, w12.txt, w13.txt, w14.txt, w15.txt, w16.txt, w17.txt, w18.txt, w2.txt, w3.txt, w4.txt, w5.txt, w6.txt, w7.txt, w8.txt, w9.txt
Case Name Status Exec Time Memory
01.txt AC 199 ms 25808 KB
02.txt TLE 3158 ms 277784 KB
03.txt AC 1700 ms 192460 KB
04.txt AC 413 ms 61596 KB
05.txt AC 1351 ms 148608 KB
06.txt AC 584 ms 83284 KB
07.txt AC 759 ms 78264 KB
08.txt AC 1551 ms 193520 KB
09.txt AC 763 ms 90932 KB
10.txt AC 763 ms 91124 KB
11.txt AC 1249 ms 142716 KB
12.txt AC 558 ms 101996 KB
13.txt AC 1036 ms 121524 KB
14.txt AC 1197 ms 139824 KB
15.txt AC 926 ms 97160 KB
16.txt AC 1160 ms 136000 KB
17.txt AC 1371 ms 148572 KB
18.txt AC 788 ms 112424 KB
19.txt AC 1234 ms 137544 KB
20.txt AC 1394 ms 163904 KB
21.txt TLE 3162 ms 363152 KB
22.txt MLE 2825 ms 358748 KB
23.txt AC 1492 ms 214000 KB
24.txt AC 1304 ms 145096 KB
25.txt AC 733 ms 131824 KB
26.txt AC 1032 ms 122092 KB
27.txt AC 1220 ms 139660 KB
28.txt AC 1269 ms 136624 KB
29.txt AC 1233 ms 146736 KB
30.txt AC 1273 ms 136272 KB
31.txt AC 1064 ms 178036 KB
32.txt AC 1238 ms 137900 KB
33.txt AC 1297 ms 138832 KB
34.txt AC 1610 ms 189672 KB
35.txt AC 496 ms 72960 KB
36.txt AC 826 ms 107176 KB
37.txt AC 1563 ms 184748 KB
38.txt MLE 1275 ms 299856 KB
sample_01.txt AC 151 ms 24148 KB
sample_02.txt AC 155 ms 24916 KB
sample_03.txt AC 154 ms 26196 KB
w1.txt AC 1280 ms 140560 KB
w10.txt AC 1010 ms 181168 KB
w11.txt MLE 2181 ms 344712 KB
w12.txt MLE 2370 ms 349076 KB
w13.txt MLE 1829 ms 362972 KB
w14.txt MLE 1136 ms 266612 KB
w15.txt AC 719 ms 104448 KB
w16.txt AC 604 ms 103892 KB
w17.txt AC 782 ms 162612 KB
w18.txt AC 874 ms 117584 KB
w2.txt AC 1120 ms 130724 KB
w3.txt AC 1387 ms 146816 KB
w4.txt AC 679 ms 85456 KB
w5.txt TLE 3158 ms 376740 KB
w6.txt AC 1737 ms 208684 KB
w7.txt TLE 3162 ms 355336 KB
w8.txt TLE 3162 ms 365176 KB
w9.txt TLE 3158 ms 386368 KB