Submission #1989016


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 {
          continue;
        }

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

      long hash = cur;
      if (!td.containsKey(hash) || v[2] < td.get(hash)) {
        long[] next = new long[] {cur, 0, v[2]};
        q.remove(next);
        td.put(hash, next[2]);
        q.add(next);
      }
    }

    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 5153 Byte
Status TLE
Exec Time 3162 ms
Memory 390928 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status AC
AC × 47
TLE × 4
MLE × 8
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 188 ms 28500 KB
02.txt MLE 1977 ms 356744 KB
03.txt AC 1975 ms 193936 KB
04.txt AC 402 ms 61984 KB
05.txt AC 1542 ms 152856 KB
06.txt AC 630 ms 95584 KB
07.txt AC 738 ms 87940 KB
08.txt AC 1632 ms 188292 KB
09.txt AC 831 ms 94828 KB
10.txt AC 800 ms 94060 KB
11.txt AC 1434 ms 152008 KB
12.txt AC 600 ms 95936 KB
13.txt AC 1134 ms 123804 KB
14.txt AC 1368 ms 150996 KB
15.txt AC 807 ms 122880 KB
16.txt AC 1185 ms 141540 KB
17.txt AC 1651 ms 157012 KB
18.txt AC 641 ms 108284 KB
19.txt AC 1386 ms 137032 KB
20.txt AC 1788 ms 175176 KB
21.txt TLE 3162 ms 383012 KB
22.txt MLE 2220 ms 384560 KB
23.txt AC 1545 ms 199552 KB
24.txt AC 1302 ms 148112 KB
25.txt AC 844 ms 109668 KB
26.txt AC 1091 ms 125212 KB
27.txt AC 1310 ms 150124 KB
28.txt AC 1343 ms 149824 KB
29.txt AC 1417 ms 151008 KB
30.txt AC 1293 ms 138484 KB
31.txt AC 1195 ms 179600 KB
32.txt AC 1097 ms 137240 KB
33.txt AC 1379 ms 149908 KB
34.txt MLE 1342 ms 317176 KB
35.txt AC 525 ms 76696 KB
36.txt AC 913 ms 120340 KB
37.txt MLE 1345 ms 323496 KB
38.txt AC 1438 ms 216988 KB
sample_01.txt AC 143 ms 26068 KB
sample_02.txt AC 153 ms 26068 KB
sample_03.txt AC 144 ms 26068 KB
w1.txt AC 1473 ms 156628 KB
w10.txt AC 1105 ms 127880 KB
w11.txt MLE 1383 ms 344296 KB
w12.txt MLE 1616 ms 355068 KB
w13.txt MLE 1407 ms 366816 KB
w14.txt AC 1214 ms 198084 KB
w15.txt AC 650 ms 106860 KB
w16.txt AC 652 ms 108740 KB
w17.txt AC 872 ms 113960 KB
w18.txt AC 982 ms 117712 KB
w2.txt AC 1005 ms 137264 KB
w3.txt AC 1359 ms 145500 KB
w4.txt AC 1005 ms 133116 KB
w5.txt TLE 3158 ms 387544 KB
w6.txt AC 1720 ms 179312 KB
w7.txt TLE 3158 ms 369296 KB
w8.txt TLE 3159 ms 378340 KB
w9.txt MLE 2442 ms 390928 KB