zoukankan      html  css  js  c++  java
  • [PAT] 1003 Emergency (25 分)Java

    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 N1), 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 Specification:

    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
     1 import java.util.Scanner;
     2 
     3 public class Main{
     4   static int n,m,c1,c2;
     5   static int[] weight = new int[500];
     6   static int[][] map = new int[500][500];
     7   static int minpath = 9999;
     8   static int anspath = 0;
     9   static int answeights = 0;
    10   static boolean [][] visit = new boolean[500][500];
    11   
    12   public static void main(String[] args){
    13     Scanner sc = new Scanner(System.in);
    14     n = sc.nextInt();
    15     m = sc.nextInt();
    16     c1 = sc.nextInt();
    17     c2 = sc.nextInt();
    18     for(int i = 0; i < n; i++){
    19       weight[i] = sc.nextInt();
    20     }
    21     for(int i = 0;i < m; i++){
    22       int x = sc.nextInt();
    23       int y = sc.nextInt();
    24       int z = sc.nextInt();
    25       map[x][y] = map[y][x] = z;
    26     }
    27     dfs(c1, 0, weight[c1]);
    28     System.out.printf("%d %d", anspath, answeights);
    29   }
    30   static void dfs(int start, int path,int weights){
    31     if (start ==c2){
    32       if(path < minpath){
    33         minpath = path;
    34         anspath = 1;
    35         answeights = weights;
    36       }else if(path == minpath){
    37         anspath++;
    38         if(weights > answeights){
    39           answeights = weights;
    40         }
    41       }return;
    42     }
    43     if(path > minpath) return;
    44     for(int i = 0;i<n;i++){
    45       if(visit[start][i] == false && map[start][i] != 0){
    46         visit[start][i] = visit[i][start] = true;
    47         dfs(i, path + map[start][i], weights + weight[i]);
    48         visit[start][i] = visit[i][start] = false;
    49       }
    50     }
    51     return;
    52   }
    53 }
  • 相关阅读:
    Mybatis plus 多表连接分页查询
    webstorm自动格式化.vue文件并符合Eslint
    Selenium python爬虫
    Cent OS防火墙配置端口开放
    开发Hexo主题(一)
    谷歌开发者主页回归
    个人博客网站
    linux搭建ftp
    putty之pscp上传文件
    送走了最好的兄弟 收到上交复试通知
  • 原文地址:https://www.cnblogs.com/PureJava/p/10497920.html
Copyright © 2011-2022 走看看