PAT(甲) 1003. Emergency (25)

1003. Emergency (25)

题目地址: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.


  • 输入格式
    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.

  • 输出格式
    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.


解题方法:
本题可以用迪杰斯特拉算法,求起点和终点的最短路,这一过程并不复杂,但是有几个易错点需要注意。


易错点:
1.在计算最短路径的相同条数时,不能只用一个数来累加,而要建一个数组。当前点的最短路径相同的条数与前一个点最短路径相同的条数有关:

  • 如果经过前一个结点V后,到达W点的路径长度与原来的最长长度相同,则W点的最短路径条数为
    SameRouteNum[W] += SameRouteNum[V]
  • 如果经过前一个结点V后,到达W点的路径长度比原来的最长长度短,则W点的最短路径条数为
    SameRouteNum[W] = SameRouteNum[V]

2.在计算最多救援队的时候,需要用到两个数组,一个用来存原来每个城市的救援队个数,另一个用来累计救援队个数,如果只用一个,会出现重复加的情况。


程序:

#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 501
#define INFINITY 65536
typedef int Vertex;
typedef int Weight;
int G[MaxVertexNum][MaxVertexNum];  /* 记录结点信息 */
int dist[MaxVertexNum];     /* 记录起点到其他点的距离 */
int collection[MaxVertexNum];   /* 记录是否收集该城市 */
int TeamNum[MaxVertexNum] = {0};    /* 到达i-th城市后的最大救援队数目 */
int SameRouteNum[MaxVertexNum]; /* 相同路线长度的个数 */


Vertex FindMin(int dist[], int collection[], int N)
{   /* 寻找当前集合中的最短路径 */
    Vertex Min;
    int MinDistance = INFINITY;
    for (Vertex v = 0; v < N; v++)
        if (collection[v] == false && dist[v] < MinDistance)
        {   /* 如果出现未收入集合中的点且当前距离最短就更新最短路和顶点下标 */
            Min = v;
            MinDistance = dist[v];
        }
    if (MinDistance < INFINITY)
        return Min;
    else
        return -1;
}

void Dijkstra(int Start, int N, int End, int RescueNum[])
{   /* 迪杰斯特拉算法求单源最短路 */
    for (Vertex v = 0; v < N; v++)  /* 初始化起点与其他点的路径长度 */
        dist[v] = G[Start][v];
    for (int i = 0; i < N; i++)
    {   /* 初始化数组 */
        collection[i] = false;
        TeamNum[i] = RescueNum[i];
    }
    dist[Start] = 0;    /* 起点的距离为0 */
    SameRouteNum[Start] = 1;

    while (1)
    {
        Vertex V = FindMin(dist, collection, N);
        if (V == -1 || V == End)    /* 如果找不到最短边或者目的城市进入集合就退出 */
            break;
        collection[V] = true;   /* 将该点收入集合 */
        for (Vertex W = 0; W < N; W++)
            if (collection[W] == false && dist[W] >= dist[V] + G[V][W])
            {   /* 如果当前点未被收录集合并且通过V到达W的路径更短或相等 */
                int len = dist[V] + G[V][W];
                if (len == dist[W])
                {   
                    SameRouteNum[W] += SameRouteNum[V];
                    if (TeamNum[V] + RescueNum[W] > TeamNum[W])
                        TeamNum[W] = TeamNum[V] + RescueNum[W];
                }
                else if (len < dist[W])
                {
                    SameRouteNum[W] = SameRouteNum[V];
                    dist[W] = len;
                    TeamNum[W] = TeamNum[V] + RescueNum[W];
                }
            }
    }
}

int main(int argc, char const *argv[])
{
    int N, M, Start, End, i, j;
    scanf("%d %d %d %d", &N, &M, &Start, &End);
    int RescueNum[N];
    for (i = 0; i < N; i++)
        scanf("%d", &RescueNum[i]);
    for (i = 0; i < N; i++) /* 初始化图 */
        for (j = 0; j < N; j++)
            G[i][j] = INFINITY;
    for (i = 0; i < M; i++) /* 输入结点信息 */
    {
        Vertex V1, V2;
        Weight W;
        scanf("%d %d %d", &V1, &V2, &W);
        G[V1][V2] = W;
        G[V2][V1] = W;
    }
    Dijkstra(Start, N, End, RescueNum);
    printf("%d %d\n", SameRouteNum[End], TeamNum[End]);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值