[PAT]1030. Travel Plan (30)(Java实现)
来源:互联网 发布:linux系统常用命令大全 编辑:程序博客网 时间:2024/05/21 19:07
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 30 1 1 201 3 2 300 3 4 100 2 2 202 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;}}
- [PAT]1030. Travel Plan (30)(Java实现)
- Python实现Pat 1030. Travel Plan (30)
- 1030. Travel Plan (30)-PAT
- 1030. Travel Plan (30) PAT
- PAT 1030. Travel Plan (30)
- PAT 1030. Travel Plan (30)
- PAT 1030. Travel Plan (30)
- 【PAT】1030. Travel Plan (30)
- pat 1030. Travel Plan (30)
- PAT 1030. Travel Plan (30)
- PAT 1030. Travel Plan
- 【PAT】1030. Travel Plan
- PAT Advanced Level 1030. Travel Plan (30)(Java and C++)
- 【PAT Advanced Level】1030. Travel Plan (30)
- PAT 1030. Travel Plan (30) 图论
- PAT A 1030. Travel Plan (30)
- PAT (Advanced) 1030. Travel Plan (30)
- PAT(A) - 1030. Travel Plan (30)
- python 从文件夹抽取图片另存
- 有关数组中出现不成对数字的例题解析
- IT忍者神龟之Windows Redis默认配置文件,Redis配置不生效解决方案
- 使用Navicat远程阿里云服务器上MySQL
- 在ie浏览器下载文件乱码解决
- [PAT]1030. Travel Plan (30)(Java实现)
- 装饰者模式(java实现)
- Java Queue系列源码解析
- 深入理解分布式事务
- Javase—String类习题
- 笛卡尔积、除、(外)连接等重要关系代数求解方法 概述
- Eclipse启动问题:An error is occurred
- Python的使用总结-1
- IT忍者神龟之Linux Redis安装,Linux如何安装Redis,Linux Redis自动启动,Redis开机启动