Advertisement
Guest User

Untitled

a guest
May 17th, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.reflect.*;
  4.  
  5.  
  6. public class Main {
  7.  
  8.     static long CURRENT_TIME_NANO = System.nanoTime();
  9.  
  10.     public static void main(String[] args) throws Exception {
  11.  
  12.         int n = next();
  13.         int m = next();
  14.  
  15.         int[] a = new int[m];
  16.         int[] b = new int[m];
  17.         int[] c = new int[m];
  18.  
  19.         for (int i = 0; i < m; i++) {
  20.             a[i] = next() - 1;
  21.             b[i] = next() - 1;
  22.             c[i] = next() * 10000 - 1;
  23.         }
  24.  
  25.         long[][] len = new long[n][n];
  26.         long inf = 1000000000000L;
  27.         for (long[] l : len) Arrays.fill(l, inf);
  28.  
  29.         for (int i = 0; i < m; i++) {
  30.             len[a[i]][b[i]] = Math.min(len[a[i]][b[i]], c[i]);
  31.             len[b[i]][a[i]] = Math.min(len[b[i]][a[i]], c[i]);
  32.         }
  33.  
  34.         long[] l = new long[n];
  35.         Arrays.fill(l, inf);
  36.         boolean[] take = new boolean[n];
  37.         l[0] = 0;
  38.  
  39.         for (int i = 0; i < n; i++) {
  40.             int v = -1;
  41.             for (int j = 0; j < n; j++)
  42.                 if (!take[j]) if (v == -1 || l[j] < l[v]) v = j;
  43.  
  44.             take[v] = true;
  45.             for (int j = 0; j < n; j++) if (!take[j])
  46.                 l[j] = Math.min(l[j], l[v] + len[v][j]);
  47.         }
  48.  
  49.         long val = l[n - 1];
  50.         int count = 0;
  51.         while (val % 10000 != 0) {count++; val++;}
  52.  
  53.         out.println((val / 10000) + " " + count);
  54.  
  55.  
  56.         out.close();
  57.     }
  58.    
  59.     static void printtime() {System.out.println((System.nanoTime() - CURRENT_TIME_NANO)*1e-9);}
  60.     static void nexttime() {printtime(); CURRENT_TIME_NANO = System.nanoTime();}
  61.     static PrintWriter out = new PrintWriter(System.out);
  62.    
  63.     static BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
  64.     static StringTokenizer in = new StringTokenizer("");
  65.  
  66.     static String nextToken() throws Exception {
  67.         if (!in.hasMoreTokens()) in = new StringTokenizer(bufferedreader.readLine());
  68.         return in.nextToken();
  69.     }
  70.  
  71.     static int next()  throws Exception {return Integer.parseInt(nextToken());};
  72.     static int[] next(int n) throws Exception {
  73.         int[] x = new int[n];
  74.         for (int i = 0; i < n; i++) x[i] = next();
  75.         return x;
  76.     }
  77.     static int[][] next(int n, int m) throws Exception {
  78.         int[][] x = new int[n][];
  79.         for (int i = 0; i < n; i++) x[i] = next(m);
  80.         return x;
  81.     }
  82.  
  83.     static long nextl() throws Exception {return Long.parseLong(nextToken());};
  84.     static long[] nextl(int n) throws Exception {
  85.         long[] x = new long[n];
  86.         for (int i = 0; i < n; i++) x[i] = nextl();
  87.         return x;
  88.     }
  89.     static long[][] nextl(int n, int m) throws Exception {
  90.         long[][] x = new long[n][];
  91.         for (int i = 0; i < n; i++) x[i] = nextl(m);
  92.         return x;
  93.     }
  94.  
  95.     static double nextd() throws Exception {return Double.parseDouble(nextToken());};
  96.     static double[] nextd(int n) throws Exception {
  97.         double[] x = new double[n];
  98.         for (int i = 0; i < n; i++) x[i] = nextd();
  99.         return x;
  100.     }
  101.     static double[][] nextd(int n, int m) throws Exception {
  102.         double[][] x = new double[n][];
  103.         for (int i = 0; i < n; i++) x[i] = nextd(m);
  104.         return x;
  105.     }
  106.  
  107.     static String nextline() throws Exception {
  108.         in = new StringTokenizer("");
  109.         return bufferedreader.readLine();
  110.     }
  111.  
  112.     static void sout(String s) {System.out.println(s);}
  113.     static void sout(long x) {System.out.println(x);}
  114.     static void sout(int[] x) {for (int xx : x) System.out.print(xx + " "); System.out.println();}
  115.     static void sout(long[] x) {for (long xx : x) System.out.print(xx + " "); System.out.println();}
  116.     static void sout(int[][] x) {for (int[] xx : x) sout(xx); System.out.println();}
  117.     static void sout(long[][] x) {for (long[] xx : x) sout(xx); System.out.println();}
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement