zoukankan      html  css  js  c++  java
  • PAT 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 

    最短路径搜索,比较麻烦的是,必须统计所有最短路径的数量,所以在找到相同cost路径时必须把路径的数量相加,体现在代码的第56~60之间。

    代码

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <limits.h>
     4 
     5 int dijkstra(int,int,int&,int&);
     6 int N,M,c1,c2;
     7 int num_rescue[500];
     8 int map[500][500];
     9 int cost[500];
    10 int num_path[500];
    11 int num_res_gather[500];
    12 int flag[500];
    13 int main()
    14 {
    15     int i;
    16     int s,e,c;    
    17     while (scanf("%d%d%d%d",&N,&M,&c1,&c2) != EOF){
    18         memset(num_rescue,0,sizeof(num_rescue));
    19         memset(map,0,sizeof(map));
    20         for(i=0;i<N;++i)
    21             cost[i] = INT_MAX;
    22         memset(num_path,0,sizeof(num_path));
    23         memset(num_res_gather,0,sizeof(num_res_gather));
    24         memset(flag,0,sizeof(flag));
    25         for (i=0;i<N;++i){
    26             scanf("%d",&num_rescue[i]);
    27         }
    28         for (i=0;i<M;++i){
    29             scanf("%d%d%d",&s,&e,&c);
    30             map[s][e] = c;
    31             map[e][s] = c;
    32         }
    33         cost[c1] = 0;
    34         num_path[c1] = 1;
    35         num_res_gather[c1] = num_rescue[c1];
    36         flag[c1] = 1;
    37         int path,rescue;
    38         dijkstra(c1,c2,path,rescue);
    39         printf("%d %d ",path,rescue);
    40     }
    41     return 0;
    42 }
    43 
    44 int dijkstra(int start,int end,int& path,int& rescue)
    45 {
    46     int s = start;
    47     while(!flag[end]){
    48         int i;
    49         for (i=0;i<N;++i){
    50             if(!flag[i] && map[s][i]){
    51                 if (cost[s] + map[s][i] < cost[i]){
    52                     cost[i] = cost[s] + map[s][i];
    53                     num_path[i] = num_path[s];
    54                     num_res_gather[i] = num_res_gather[s] + num_rescue[i];
    55                 }
    56                 else if (!flag[i] && cost[s] + map[s][i] == cost[i]){
    57                     num_path[i] = num_path[s] + num_path[i];
    58                     num_res_gather[i] = ( num_res_gather[i] > num_res_gather[s] + num_rescue[i]? 
    59                         num_res_gather[i] : num_res_gather[s]+num_rescue[i]);
    60                 }
    61             }
    62         }
    63         i = 0;
    64         while(flag[i++]);
    65         int min_cost = cost[--i];
    66         s = i;
    67         for (++i;i<N;++i){
    68             if (!flag[i] && cost[i] < min_cost){
    69                 min_cost = cost[i];
    70                 s = i;
    71             }
    72         }
    73         flag[s] = 1;
    74     }
    75     path = num_path[end];
    76     rescue = num_res_gather[end];
    77     return cost[end];
    78 }
  • 相关阅读:
    【读书笔记】iOS-简单的数据驱动程序
    数据结构—单链表(类C语言描写叙述)
    使用Hadoop ACL 控制訪问权限
    Iocomp控件之数字显示【图文】
    维护的JSP站点数据丢失
    Simditor用法
    Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)
    Deferred Rendering(三)反锯齿和半透明问题
    iOS Code Sign error: Provisioning profile can&#39;t be found 解决方式
    spring
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1003.html
Copyright © 2011-2022 走看看