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 Int(), 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.(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 CityNum()];costTo = new CityNum()];pathTo = new CityNum()];visited = new CityNum()];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) {V()].add(e);W()].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 = st = 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;}}
本文发布于:2024-01-30 15:28:37,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170659971720989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |