1030. Travel Plan (30)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20Sample Output
0 2 3 3 40
package go.jacob.day1123;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 1030. Travel Plan (30)
* @author Jacob
*
*/
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(), M = sc.nextInt(), S = sc.nextInt(), D = sc.nextInt();
// 生成图
Graph graph = new Graph(N, M);
for (int i = 0; i < M; i++) {
Edge e = new Edge(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
graph.addEdge(e);
}
Dijkstra d = new Dijkstra(graph, S, D);
d.getPath();
ArrayList<Integer> res = getRes(d, D);
for (int i = res.size() - 1; i >= 0; i--) {
System.out.print(res.get(i) + " ");
}
System.out.println(d.distTo[D] + " " + d.costTo[D]);
sc.close();
}
private static ArrayList<Integer> getRes(Dijkstra d, int des) {
ArrayList<Integer> list = new ArrayList<Integer>();
int tmp = des;
while (tmp != -1) {
list.add(tmp);
tmp = d.pathTo[tmp];
}
return list;
}
}
/*
* 迪杰斯特拉算法类
*/
class Dijkstra {
Graph g;
public Integer[] distTo;// 节点到起点的距离
public Integer[] costTo;// 节点到起点的开销
public Integer[] pathTo;
private boolean[] visited;// 节点是否被访问
private int src;
private int des;
public Dijkstra(Graph g, int src, int des) {
this.g = g;
this.src = src;
this.des = des;
distTo = new Integer[g.getCityNum()];
costTo = new Integer[g.getCityNum()];
pathTo = new Integer[g.getCityNum()];
visited = new boolean[g.getCityNum()];
for (int i = 0; i < g.getCityNum(); i++) {
distTo[i] = Integer.MAX_VALUE;
costTo[i] = Integer.MAX_VALUE;
visited[i] = false;
}
}
public void getPath() {
distTo[src] = 0;
costTo[src] = 0;
pathTo[src] = -1;
int city = src;
while (true) {
if (city == -1)
break;
visited[city] = true;
relax(city);
int min = Integer.MAX_VALUE, index = -1;
for (int i = 0; i < g.getCityNum(); i++) {
if (!visited[i] && distTo[i] < min) {
min = distTo[i];
index = i;
}
}
city = index;
}
}
public void relax(int v) {
List<Edge> adj = g.adj(v);
for (Edge e : adj) {
if (distTo[e.either(v)] > distTo[v] + e.getDist()) {
distTo[e.either(v)] = distTo[v] + e.getDist();
pathTo[e.either(v)] = v;
costTo[e.either(v)] = costTo[v] + e.getCost();
} else if (distTo[e.either(v)] == distTo[v] + e.getDist()) {
if (costTo[e.either(v)] > costTo[v] + e.getCost()) {
pathTo[e.either(v)] = v;
costTo[e.either(v)] = costTo[v] + e.getCost();
}
}
}
}
}
/*
* 图类
*/
class Graph {
private int cityNum, highwayNum;
private ArrayList<Edge>[] bag;
@SuppressWarnings("unchecked")
public Graph(int cityNum, int highwayNum) {
this.cityNum = cityNum;
this.highwayNum = highwayNum;
bag = new ArrayList[cityNum];
for (int i = 0; i < cityNum; i++) {
bag[i] = new ArrayList<Edge>();
}
}
public List<Edge> adj(int v) {
return bag[v];
}
public void addEdge(Edge e) {
bag[e.getV()].add(e);
bag[e.getW()].add(e);
}
public int getCityNum() {
return cityNum;
}
public int getHighwayNum() {
return highwayNum;
}
public ArrayList<Edge>[] getBag() {
return bag;
}
}
/*
* 加权边类
*/
class Edge {
private int v;
private int w;
private int dist;
private int cost;
public Edge(int v, int w, int dist, int cost) {
super();
this.v = v;
this.w = w;
this.dist = dist;
this.cost = cost;
}
public int either(int i) {
return i == v ? w : v;
}
public int getV() {
return v;
}
public int getW() {
return w;
}
public int getDist() {
return dist;
}
public int getCost() {
return cost;
}
}