HDU 2544 最短路

原题

Description

就是最短路

Algorithm

Dijkstra

我就用最水的 未加优化的做了一下 理论时间复杂度O(V^2)
V为顶点数
后来想想光一开始赋值INF就用了V^2实在不划算
就做了个hasCost的优化,空间换时间,不过也没有快多少
还是要学优先队列的优化啊
V2.1用了优先队列优化 不过比V1.1还慢 = =
不过好歹会用了JAVA的pq

Bellman-Ford

V3.0
理论上最慢 实际上最快 = =

Code

V3.0

import java.util.Scanner;
class Edge {
  int from;
  int to;
  int cost;
  Edge(int a, int b, int c) {
    from = a;
    to = b;
    cost = c;
  }
}
public class Main {
  public static void main(String[] args) {
    final int INF = Integer.MAX_VALUE / 2;
    Scanner cin = new Scanner(System.in);
    for (;;) {
      int n = cin.nextInt();
      int m = cin.nextInt();
      if (n == 0) break;
      int[] d = new int[n];
      for (int i = 0; i < n; i++) {
        d[i] = INF;
      }
      Edge[] g = new Edge[2 * m];
      for (int i = 0; i < m; i++) {
        int a = cin.nextInt() - 1;
        int b = cin.nextInt() - 1;
        int c = cin.nextInt();
        g[i] = new Edge(a, b, c);
        g[i + m] = new Edge(b, a, c);
      }
      d[0] = 0;
      for (;;) {
        boolean update = false;
        for (Edge e : g) {
          if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
            d[e.to] =  d[e.from] + e.cost;
            update = true;
          }
        }
        if (!update) break;
      }
      System.out.println(d[n - 1]);
    }
  }
}

V2.0

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
class Edge {
  Edge(int a, int b) {
    to = a;
    cost = b;
  }
  int to;
  int cost;
}
class P {
  int dis; //最短距离
  int i; //顶点编号
  P(int a, int b) {
    dis = a;
    i = b;
  }
}
public class Main {
  public static void main(String[] args) {
    final int INF = Integer.MAX_VALUE / 2;
    Scanner cin = new Scanner(System.in);
    for (;;) {
      int n = cin.nextInt();
      int m = cin.nextInt();
      if (n == 0) break;
      PriorityQueue<P> pq = new PriorityQueue<P>((o1, o2) -> { if (o1.dis < o2.dis) return -1; else return 1; }); ArrayList<Edge>[] g = new ArrayList[n]; for (int i = 0; i < n; i++) { g[i] = new ArrayList<Edge>(); } for (int i = 0; i < m; i++) { int a = cin.nextInt() - 1; int b = cin.nextInt() - 1; int c = cin.nextInt(); g[a].add(new Edge(b, c)); g[b].add(new Edge(a, c)); } int[] d = new int[n]; Arrays.fill(d, INF); d[0] = 0; pq.add(new P(0, 0)); while (!pq.isEmpty()) { P p = pq.poll(); int v = p.i; if (d[v] < p.dis) continue; for (Edge e : g[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; pq.add(new P(d[e.to], e.to)); } } } System.out.println(d[n - 1]); } } } 

V1.0

import java.util.Arrays;
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    for (;;) {
      int n = cin.nextInt();
      int m = cin.nextInt();
      if (n == 0) break;
      int[][] cost = new int[n][n];
      for (int i = 0; i < n; i++) {
        Arrays.fill(cost[i], Integer.MAX_VALUE / 2);
        cost[i][i] = 0;
      }
      for (int i = 0; i < m; i++) {
        int a = cin.nextInt() - 1;
        int b = cin.nextInt() - 1;
        int c = cin.nextInt();
        cost[a][b] = c;
        cost[b][a] = c;
      }
      int[] d = new int[n];
      Arrays.fill(d, Integer.MAX_VALUE / 2);
      boolean[] used = new boolean[n];
      d[0] = 0;
      for (;;) {
        int v = -1;
        for (int u = 0; u < n; u++) {
          if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if (v == -1) break;
        used[v] = true;
        for (int u = 0; u < n; u++) {
          d[u] = Math.min(d[u], d[v] + cost[v][u]);
        }
      }
      System.out.println(d[n - 1]);
    }
  }
}

V1.1

import java.util.Arrays;
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    final int INF = Integer.MAX_VALUE / 2;
    Scanner cin = new Scanner(System.in);
    for (;;) {
      int n = cin.nextInt();
      int m = cin.nextInt();
      if (n == 0) break;
      int[][] cost = new int[n][n];
      boolean[][] hasCost = new boolean[n][n];
      for (int i = 0; i < m; i++) {
        int a = cin.nextInt() - 1;
        int b = cin.nextInt() - 1;
        int c = cin.nextInt();
        cost[a][b] = c;
        cost[b][a] = c;
        hasCost[a][b] = true;
        hasCost[b][a] = true;
      }
      int[] d = new int[n];
      Arrays.fill(d, INF);
      boolean[] used = new boolean[n];
      d[0] = 0;
      for (;;) {
        int v = -1;
        for (int u = 0; u < n; u++) {
          if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if (v == -1) break;
        used[v] = true;
        for (int u = 0; u < n; u++) {
          int w = d[v] + cost[v][u];
          if (!hasCost[v][u]) w = INF;
          d[u] = Math.min(d[u], w);
        }
      }
      System.out.println(d[n - 1]);
    }
  }
}

你可能感兴趣的:(HDU 2544 最短路)