1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) – the number of cities (and the cities are numbered from 0 to N-1), M – the number of roads, C1 and C2 – the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4
题目大意:n个城市m条路,每个城市有救援小组,所有的边的边权已知。给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和。如果有多条就输出点权(城市救援小组数目)最大的那个~
PS:感谢github用户@fs19910227提供的pull request~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.LinkedList; import java.util.TreeSet; public class Main { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static class Road implements Comparable<Road> { int fromCity, toCity, distance; Road(int fromCity, int toCity, int distance) { this.fromCity = fromCity; this.toCity = toCity; this.distance = distance; } @Override public int compareTo(Road o) { int compare = distance - o.distance; if (compare == 0) { compare = toCity - o.toCity; return compare == 0 ? fromCity - o.fromCity : compare; } else { return compare; } } } static class Node implements Comparable<Node> { int id, rescueTeams = 0, roadLength = 0; Node(int id, int rescueTeams) { this.id = id; this.rescueTeams = rescueTeams; } @Override public int compareTo(Node o) { int compare = roadLength - o.roadLength; return compare == 0 ? hashCode() - o.hashCode() : compare; } @Override public String toString() { return "Node{" + "id=" + id + ", rescueTeams=" + rescueTeams + ", roadLength=" + roadLength + '}'; } } static class CityMap { final int cityNumbers, start_city, end_city, roadNumbers; final int[] rescues; final LinkedList<Road>[] roads; CityMap() throws IOException { int[] ints = readLine(); this.cityNumbers = ints[0]; this.roadNumbers = ints[1]; this.start_city = ints[2]; this.end_city = ints[3]; rescues = readLine(); roads = new LinkedList[cityNumbers]; for (int i = 0; i < cityNumbers; i++) { roads[i] = new LinkedList<>(); } for (int i = 0; i < roadNumbers; i++) { //读入路径 ints = readLine(); int start = ints[0], end = ints[1], length = ints[2]; roads[start].add(new Road(start, end, length)); roads[end].add(new Road(end, start, length)); } } } private static int[] readLine() throws IOException { return Arrays.stream(reader.readLine().split(" ")) .mapToInt(Integer::valueOf) .toArray(); } public static void main(String[] args) throws IOException { final CityMap cityMap = new CityMap(); final int start_city = cityMap.start_city; final int end_city = cityMap.end_city; int[] rescues = cityMap.rescues; boolean[] visited = new boolean[cityMap.cityNumbers]; TreeSet<Node> nodes = new TreeSet<>(); nodes.add(new Node(start_city, rescues[start_city])); int max_counts = 0; int max_rescue_teams = 0; int min_load_length = Integer.MAX_VALUE; while (!nodes.isEmpty()) { Node node = nodes.pollFirst(); visited[node.id] = true; if (node.roadLength > min_load_length) break; if (node.id == end_city) { min_load_length = node.roadLength; max_counts++; max_rescue_teams = Math.max(max_rescue_teams, node.rescueTeams); } for (Road road : cityMap.roads[node.id]) { if (!visited[road.toCity]) { Node newNode = new Node(road.toCity, node.rescueTeams + rescues[road.toCity]); newNode.roadLength = node.roadLength + road.distance; nodes.add(newNode); } } } System.out.println(max_counts + " " + max_rescue_teams); } } |