2019 cattle off the holiday season team 3 - Roadblocks

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Links: https://ac.nowcoder.com/acm/contest/945/D
Source: Cattle-off network

Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1…N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input

Line 1: Two space-separated integers: N and R
Lines 2…R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N
示例1

Input Sample

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Output Sample

450

说明
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

Problem-solving ideas

Seeking short-circuits: determined using Dijkstra shortest path from the two, two arrays, traversing through all sides, from each edge (u, v), retrieve all dis1 [u] + EDGE (u, v) + dis2 [v]> minimum under minDis (start and end points of the shortest distance) conditions, even solutions

AC Code

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-06-25 T 10:43:37.961 +08:00
 */
package ACMProblems.QianDaoTi;

import java.util.*;

import static ACMProblems.ACMIO.*;

/*
 * 链接:https://ac.nowcoder.com/acm/contest/945/D
 * 来源:牛客网
 *
 * ## Description
 * Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
 * The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
 * The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
 * ## Input
 * Line 1: Two space-separated integers: N and R
 * Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
 *
 * ## Output
 * Line 1: The length of the second shortest path between node 1 and node N
 * 示例1
 * ## Input Sample
 * >4 4
 * 1 2 100
 * 2 4 200
 * 2 3 250
 * 3 4 100
 *
 * ## Output Sample
 * >450
 * >
 * **说明**
 * Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
 */
public class SecondShortest {
    static class VertexNode {
        private int id;
        private int weight;

        public VertexNode(int id, int weight) {
            this.id = id;
            this.weight = weight;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        @Override
        public boolean equals(Object obj) {
            return this.id == ((VertexNode) obj).id;
        }
    }

    static class Edge {
        int from;
        int to;
        int weight;

        public Edge(int from, int to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }

        @Override
        public String toString() {
            return "(" + from + " -> " + to + ", " + weight + ")";
        }
    }

    private static int vertexNum;
    private static ArrayList<VertexNode>[] lists;

    /**
     * Add one edge into the graph. The weight will be changed if the edge already exists.
     *
     * @param a      Start
     * @param b      End
     * @param weight The weight of the edge from a to b
     */
    public static void addEdge(int a, int b, int weight) {
        assert a < vertexNum && b < vertexNum;
        lists[a].add(new VertexNode(b, weight));
        lists[b].add(new VertexNode(a, weight));
        edges[index++] = new Edge(a, b, weight);
        edges[index++] = new Edge(b, a, weight);
    }

    private static int[] dijkstra(int a, int b) {
        assert a < vertexNum && b < vertexNum;
        int[] dis = new int[vertexNum];
        boolean[] visited = new boolean[vertexNum];
        // The heap used to find the closest vertex.
        PriorityQueue<VertexNode> q = new PriorityQueue<>(vertexNum, Comparator.comparingInt(VertexNode::getWeight));
        //initialize
        q.add(new VertexNode(a, 0));
        Arrays.fill(dis, 0x3f3f3f3f);
        dis[a] = 0;
        while (!q.isEmpty()) {
            // Current vertex.
            VertexNode v = q.poll();
            int currID = v.getId();
            //Mark as visited.
            visited[currID] = true;
            //for each neighbor
            for (VertexNode dest : lists[currID]) {
                //if there is a shorter path
                if (!visited[dest.getId()] && dis[currID] + dest.getWeight() < dis[dest.getId()]) {
                    dis[dest.getId()] = dis[currID] + dest.getWeight();
                    q.add(new VertexNode(dest.getId(), dis[dest.getId()]));
                }
            }
        }
        return dis;
    }

    static Edge[] edges;
    static int index = 0;

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        setStream(System.in);
        vertexNum = nextInt();
        int edgeNum = nextInt();
        edges = new Edge[edgeNum * 2];
        lists = new ArrayList[vertexNum];
        for (int i = 0; i < vertexNum; ++i)
            lists[i] = new ArrayList<>();
        for (int i = 0; i < edgeNum; ++i) {
            int a = nextInt() - 1;
            int b = nextInt() - 1;
            int weight = nextInt();
            addEdge(a, b, weight);
        }
        int[] dis1 = dijkstra(0, vertexNum - 1);
        int[] dis2 = dijkstra(vertexNum - 1, 0);
        int minDis = dis1[vertexNum - 1];
        int currMin = Integer.MAX_VALUE;
        for (Edge edge : edges) {
            int from = edge.from;
            int to = edge.to;
            int weight = edge.weight;
            int foo = dis1[from] + dis2[to] + weight;
            if (foo > minDis)
                currMin = Math.min(foo, currMin);
        }
        System.out.println(currMin);
    }
}

Guess you like

Origin blog.csdn.net/weixin_44090305/article/details/93599787
Recommended