[Java] 1030. Travel Plan (30)-PAT甲级

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 Input

4 5 0 3

0 1 1 20

1 3 2 30

0 3 4 10

0 2 2 20

2 3 1 20

Sample Output

0 2 3 3 40

题目大意:求起点到终点的最短路径最短距离和花费,要求首先路径最短,其次花费最少,要输出完整路径

PS:感谢github用户 @fs19910227 提供的pull request~

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.PriorityQueue;
 
public class Main {
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 
    static class Info {
        int distance, cost;
 
        Info(int distance, int cost) {
            this.distance = distance;
            this.cost = cost;
        }
    }
 
    static class Graph {
        private final int vertex, edges, end, start;
        private final Info[][] tables;
 
        Graph() throws IOException {
            String[] split = reader.readLine().split(" ");
            this.vertex = Integer.valueOf(split[0]);
            this.edges = Integer.valueOf(split[1]);
            this.start = Integer.valueOf(split[2]);
            this.end = Integer.valueOf(split[3]);
 
            tables = new Info[vertex][vertex];
            for (int i = 0; i < edges; i++) {
                String[] lineInfo = reader.readLine().split(" ");
                int s = Integer.valueOf(lineInfo[0]);
                int e = Integer.valueOf(lineInfo[1]);
                int distance = Integer.valueOf(lineInfo[2]);
                int cost = Integer.valueOf(lineInfo[3]);
                tables[s][e] = new Info(distance, cost);
                tables[e][s] = new Info(distance, cost);
            }
        }
 
        Node bfs() {
            Node node = new Node(start);
            boolean[] checkTable = new boolean[vertex];
            PriorityQueue<Node> queue = new PriorityQueue<>();
            queue.add(node);
            while (!queue.isEmpty()) {
                Node poll = queue.poll();
                if (poll.id == end) {
                    return poll;
                }
                checkTable[poll.id] = true;
                for (int i = 0; i < vertex; i++) {
                    Info info = tables[poll.id][i];
                    if (info != null && !checkTable[i]) {
                        Node newNode = new Node(i);
                        newNode.previous = poll;
                        newNode.distance = poll.distance + info.distance;
                        newNode.cost = poll.cost + info.cost;
                        queue.offer(newNode);
                    }
                }
            }
            return null;
        }
    }
 
    static class Node implements Comparable<Node> {
        int id, distance, cost;
        Node previous;
 
        Node(int id) {
            this.id = id;
        }
 
        @Override
        public int compareTo(Node o) {
            int compare = distance - o.distance;
            if (compare == 0) {
                compare = id - o.id;
            }
            return compare == 0 ? cost - o.cost : compare;
        }
    }
 
    public static void main(String[] args) throws IOException {
        Graph graph = new Graph();
        Node bfs = graph.bfs();
        LinkedList<String> list = new LinkedList<>();
        list.addFirst(String.valueOf(bfs.cost));
        list.addFirst(String.valueOf(bfs.distance));
        while (bfs != null) {
            list.addFirst(String.valueOf(bfs.id));
            bfs = bfs.previous;
        }
        int last = list.size() - 1;
        int start = 0;
        for (String o : list) {
            System.out.printf("%s%s", o, start == last ? "/n" : " ");
            start++;
        }
    }
}

[Java] 1030. Travel Plan (30)-PAT甲级

(随着网站访问量的激增,服务器配置只得一再升级以维持网站不“404 Not Found”,所以网站的维护费用也在不断上涨……(目前的阿里云服务器ECS+数据库RDS+域名购买+七牛云的费用是2200元/年),为了能不放弃该网站,所以我又把打赏链接放上来啦~所有打赏金额都会被记账并投入博客维护中,感谢厚爱,多多关照~)

原文 

https://www.liuchuo.net/archives/5000

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » [Java] 1030. Travel Plan (30)-PAT甲级

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址