1003. Emergency (25)(java版)

来源:互联网 发布:大公司程序员职业规划 编辑:程序博客网 时间:2024/05/17 11:33

题目:

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

思路:

这道题可以用Dijkstra算法来做,也可以用dfs来做,但是用dfs的话需要剪枝,不然会超时,这道题算是考察Dijkstra算法和dfs的基础题。

代码:

Dijkstra算法

import java.util.*;    public class Main{        static int n = 0;    static int m = 0;    static int c1 = 0;    static int c2 = 0;    static int[][] map = new int[505][505];    static int[] weight = new int[505];    static int[] dist = new int[505];    static boolean[] visit = new boolean[505];    static int[] num = new int[505];    static int[] w = new int[505];    static final int inf = 9999999;    public static void main(String[] args){          Arrays.fill(dist, inf);        int i = 0, j = 0, k = 0;        for(i=0;i<505;i++) {            Arrays.fill(map[i], inf);        }        Scanner cin = new Scanner(System.in);        n = cin.nextInt();        m = cin.nextInt();        c1 = cin.nextInt();        c2 = cin.nextInt();        for(i=0;i<n;i++) {            weight[i] = cin.nextInt();        }        for(i=0;i<m;i++) {            int x = cin.nextInt();            int y = cin.nextInt();            int z = cin.nextInt();            map[x][y] = map[y][x] = z;        }        dist[c1] = 0;        w[c1] = weight[c1];        num[c1] = 1;        for(i=0; i < n ;i++) {            int index = -1, min = inf;            for(j=0;j<n;j++) {                if(visit[j] == false && dist[j] < min) {                    min = dist[j];                    index = j;                }            }            if(index == -1) break;            visit[index] = true;            for(k=0;k<n;k++) {                if(visit[k] == false && map[index][k] != inf) {                    if(dist[index] + map[index][k] < dist[k]) {                        dist[k] = dist[index] + map[index][k];                        num[k] = num[index];                        w[k] = w[index] + weight[k];                    }                    else if(dist[index] + map[index][k] == dist[k]) {                        num[k] = num[k] + num[index];                        if(w[index] + weight[k] > w[k]) {                            w[k] = w[index] + weight[k];                        }                    }                }            }        }        System.out.printf("%d %d",num[c2], w[c2]);    }  } 

dfs

import java.util.*;    public class Main{        static int n = 0;    static int m = 0;    static int c1 = 0;    static int c2 = 0;    static int[][] map = new int[505][505];    static int[] weight = new int[505];    static boolean[][] visit = new boolean[505][505];    static int anspath = 0;    static int minpath = 99999;    static int answeight = 0;    public static void main(String[] args){          int i = 0, j = 0;        Scanner cin = new Scanner(System.in);        n = cin.nextInt();        m = cin.nextInt();        c1 = cin.nextInt();        c2 = cin.nextInt();        for(i=0;i<n;i++) {            weight[i] = cin.nextInt();        }        for(i=0;i<m;i++) {            int x = cin.nextInt();            int y = cin.nextInt();            int z = cin.nextInt();            map[x][y] = map[y][x] = z;        }        dfs(c1, 0, weight[c1]);        System.out.printf("%d %d",anspath,answeight);    }    static void dfs(int start, int path, int weights) {        //System.out.printf("%d %d %d\n",start,path,weights);        if(start == c2) {            if(path < minpath) {                minpath = path;                anspath = 1;                answeight = weights;            }            else if(path == minpath) {                anspath++;                if(weights > answeight) {                    answeight = weights;                }            }            return ;        }        if(path > minpath) return ;        for(int i=0;i<n;i++) {            if(visit[start][i] == false && map[start][i] != 0) {                visit[start][i] = visit[i][start] = true;                dfs(i, path + map[start][i], weights + weight[i]);                visit[start][i] = visit[i][start] = false;            }        }        return ;    }} 
原创粉丝点击