1003 Emergency (25 分)

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 Specification:
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, C
​1
​​ and C
​2
​​ - 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 c
​1
​​ , c
​2
​​ 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 C
​1
​​ to C
​2
​​ .

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C
​1
​​ and C
​2
​​ , 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

题目大意是给出城市之间的距离以及城市的人数,然后给出起点和终点求起点到终点最短路径的条数,然后最短路径中人数最多是多少(这里最开始理解错了,以为是求最多能带多少人去没有说是在路径最短的前提下。)。
典型图的DFS遍历找出最短路径。

package xyh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
 * @author XiangYida
 * @version 2/11/19 3:40 PM
 */
public class PTA_1003 {
    private static int[]num;
    private static int[][]grip;
    //最多人数
    private static int maxHuman=-1;
    //最短路径
    private static int minLen=Integer.MAX_VALUE;
    //最短路径的条数
    private static int size=0;
    //起点,终点
    private static int begin,end;
    public static void main(String[] args) throws IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        String[] strings1=reader.readLine().split(" ");
        String[] strings2=reader.readLine().split(" ");
        //线路的条数
        int n=Integer.parseInt(strings1[1]);
        //城市数量
        int n1=Integer.parseInt(strings1[0]);
        begin=Integer.parseInt(strings1[2]);
        end=Integer.parseInt(strings1[3]);
        //城市人数
        num=new int[n1];
        for(int i = 0;i<n1;i++){
           num[i]=Integer.parseInt(strings2[i]);
        }
        grip=new int[n1][n1];
        //路线,构造图
        for(int i=0;i<n;i++){
            String[] strings3=reader.readLine().split(" ");
            int x=Integer.parseInt(strings3[0]);
            int y=Integer.parseInt(strings3[1]);
            int len=Integer.parseInt(strings3[2]);
            grip[x][y]=len;
            grip[y][x]=len;
        }
        boolean[]visited=new boolean[n1];
        dfs(begin,visited,0,0);
        System.out.println(size+" "+maxHuman);
    }
    private static void dfs(int i,boolean[]visited,int human,int len){
        visited[i]=true;
        human+=num[i];
        if(i==end){
            if(len==minLen){
                size++;
                maxHuman=Math.max(human,maxHuman);
            }else if(len<minLen){
                size=1;
                minLen=len;
                maxHuman=human;
            }
            return;
        }
        for (int w=firstVertex(i);w>=0;w=nextVertex(i,w))
            if (!visited[w]){
                dfs(w, visited,human,len+grip[i][w]);
                visited[w]=false;
            }
    }
    private static int firstVertex(int v) {
        for (int i = 0; i < num.length; i++){
            if (grip[v][i]!=0) return i;
        }
        return -1;
    }
    private static int nextVertex(int v, int w) {
        for (int i = w + 1; i < num.length; i++){
            if (grip[v][i] != 0)
                return i;
        }
        return -1;
    }
}

你可能感兴趣的:(PAT)