zoukankan      html  css  js  c++  java
  • PAT 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
    DFS
    开始两个测试点没过发现:
    1:若1城市跟2城市之间有路则2城市与1城市之间也有路
    2:运行超时,判断路径不是最短就跳出即可。

     1 import java.util.Scanner;
     2 
     3 
     4 public class Main {
     5     static int n,m,c1,c2;
     6     static int[] a;
     7     static int[][] b;
     8     static int mix = Integer.MAX_VALUE;
     9     static int max;
    10     static int sum3;
    11     static boolean[] flag;
    12     public static void main(String[] args) {
    13         Scanner input = new Scanner(System.in);
    14         n = input.nextInt();
    15         m = input.nextInt();
    16         c1 = input.nextInt();
    17         c2 = input.nextInt();
    18         a = new int[n];
    19         for(int i=0;i<n;i++){
    20             a[i] = input.nextInt();
    21         }
    22         b = new int[n][n];
    23         for(int i=0;i<m;i++){
    24             int a1 = input.nextInt();
    25             int a2 = input.nextInt();
    26             int a3 = input.nextInt();
    27             b[a1][a2] = a3;//路径
    28             b[a2][a1] = a3;//路径
    29         }
    30         flag = new boolean[n];
    31         f(c1,0,a[c1]);
    32         System.out.println(sum3+" "+max);
    33     }
    34     public static void f(int i,int sum,int sum1){
    35         if(sum>mix){
    36             return;//不是最短的跳出
    37         }
    38         if(i==c2){
    39             if(sum<mix){
    40                 mix = sum;
    41                 sum3 = 1;
    42                 max = sum1;
    43             }else if(sum==mix){
    44                 sum3++;
    45                 if(max<sum1){
    46                     max = sum1;
    47                 }
    48             }
    49             return ;
    50         }
    51         flag[i] = true;
    52         for(int j=0;j<n;j++){
    53             if(b[i][j]!=0&&!flag[j]){
    54                 f(j,sum+b[i][j],sum1+a[j]);
    55             }
    56         }
    57         flag[i] = false;
    58     }
    59 
    60 }


  • 相关阅读:
    【产品】Pony三问
    【数据平台】阿里dataphin
    【数据中台】阿里数据中台架构
    关于 Qt 5,你所需要了解的基础知识
    Skynet 游戏服务器开发实战
    关于自然语言处理,有一本通关手册待接收
    ROS 机器人操作系统进阶实战
    面试官不讲武德,问我如何实现分布式缓存?
    高 star 开源项目来实验楼啦,深度学习强推教材
    Spring Boot 2.4.0 全新发布,还不快来实战
  • 原文地址:https://www.cnblogs.com/lolybj/p/7197975.html
Copyright © 2011-2022 走看看