PAT 1003. Emergency

来源:互联网 发布:淘宝客联盟 编辑:程序博客网 时间:2024/06/05 18:53

原题地址: http://www.patest.cn/contests/pat-a-practise/1003
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

本题先找出图的最短路径,然后用深度优先搜索找出最短路径的条数。代码如下:

package com.dye.pat.t1003;import java.util.PriorityQueue;import java.util.Scanner;public class Main {    static class Vert implements Comparable<Vert> {        public Vert() {            this.e = -1;            this.dist = -1;        }        public Vert(int i, int j) {            this.e = i;            this.dist = j;        }        private int e;        private int dist;        public int e() {            return e;        }        public int dist() {            return dist;        }        public void setE(int i) {            this.e = i;        }        public void setDist(int d) {            this.dist = d;        }        public static Vert[] getInstances(int N) {            Vert[] vert = new Vert[N];            for (int i = 0; i < N; i++) {                vert[i] = new Vert(i, 0);            }            return vert;        }        public int compare(Vert o1, Vert o2) {            if (o1.dist() > o2.dist()) {                return 1;            } else if (o1.dist() < o2.dist()) {                return -1;            } else {                return 0;            }        }        @Override        public int compareTo(Vert o2) {            if (this.dist() > o2.dist()) {                return 1;            } else if (this.dist() < o2.dist()) {                return -1;            } else {                return 0;            }        }    }    static class Res{        private int n1;        private int n2;        public Res(){            this.n1 = 0;            this.n2 = 0;        }        public int getP1(){            return n1;        }        public int getP2(){            return n2;        }        public void setP1(){            n1++;        }        public void setP2(int i){            n2 = n2+ i - min(n2,i);        }    }    public static int min(int i, int j) {        if (i < j)            return i;        else            return j;    }    //res[0] 条数,res[1] 最大人数, res[2]当前距离, res[3]当前人数, w传过来之距离    public static void dfs(int[][] weight, int from, int to, int MIN,boolean[] mark, int[] num, Res res,int w,int r){        if(from == to && w == MIN){            res.setP1();            res.setP2(r);        }        if(w > MIN){            return;        }        mark[from] = true;        for(int i=0; i<mark.length;i++){            if(weight[from][i]>0 && !mark[i])                dfs(weight, i, to, MIN, mark, num, res, w+weight[from][i], r+num[i]);        }        mark[from] = false;    }    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        String[] d = in.nextLine().split(" ");        int N = Integer.parseInt(d[0].trim());        int M = Integer.parseInt(d[1].trim());        int from = Integer.parseInt(d[2].trim());        int to = Integer.parseInt(d[3].trim());        int num[] = new int[N];        int weight[][] = new int[N][N];        for (int i = 0; i < N; i++) {            for (int j = 0; j < N; j++) {                weight[i][j] = -1;                weight[j][i] = -1;            }        }        d = in.nextLine().split(" ");        for (int i = 0; i < d.length; i++) {            num[i] = Integer.parseInt(d[i].trim());        }        for (int i = 0; i < M; i++) {            d = in.nextLine().split(" ");            int f = Integer.parseInt(d[0].trim());            int t = Integer.parseInt(d[1].trim());            weight[f][t] = Integer.parseInt(d[2].trim());            weight[t][f] = Integer.parseInt(d[2].trim());        }        in.close();        Vert vert[] = Vert.getInstances(N);        boolean mark[] = new boolean[N];        for (int i = 0; i < N; i++) {            vert[i].setE(i);            vert[i].setDist(Integer.MAX_VALUE);            mark[i] = false;        }        vert[from].setDist(0);        mark[from] = true;        PriorityQueue<Vert> pq = new PriorityQueue<Vert>();        pq.add(vert[from]);        while (!pq.isEmpty()) {            Vert vt = pq.remove();            // System.out.println("" + vt.e());            for (int i = 0; i < N; i++) {                if (weight[i][vt.e()] > 0 && !mark[i]) {                    vert[i].setDist(min(vert[i].dist(),                            weight[i][vt.e()] + vt.dist()));                    pq.add(vert[i]);                    mark[i] = true;                }                if (weight[i][vt.e()] > 0 && mark[i]) {                    if (pq.remove(vert[i])) {                        vert[i].setDist(min(vert[i].dist(), weight[i][vt.e()]                                + vt.dist()));                        pq.add(vert[i]);                    }                }            }        }        int MIN = vert[to].dist();        for(int i=0;i<N;i++){            mark[i] = false;        }        Res re = new Res();        dfs(weight, from, to, MIN, mark,num,re,0,num[from]);        System.out.println(re.getP1() + " " + re.getP2());    }}
0 0
原创粉丝点击