PAT甲级1003 Emergency (25分) [图的遍历,DFS]

1003 Emergency (25分) [图的遍历,DFS]

题目

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 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 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
题目大意

城市里的有一个地方出了紧急事故,现在需要找出在当前城市能够最快赶到事发城市的路径,同时需要赶到的人数越多越好。
输入第一行为四个数字,分别代表城市的数量N,城市间路径的数量M,当前所在的城市C1,事故发生的城市C2。
第二行给出N个数字,代表着每个城市拥有的救援队的数量。
接下去M行,每行三个数字,前两个为路径所在的两个城市,最后一个数字为路径长度。
输出需要保证C1城市和C2城市之间一定存在一条连通的路径。输出为相同的最小路径数量以及最大的救援队数量。

题目分析

要找到最短路径,考虑用到图论的迪杰斯特拉算法。先在图中找出当前路径最短的点,从这个点出发遍历图,如果找到更小的点对其进行一系列的替换操作。遍历图中的结点,找到没有被查找到过的点重复上述操作,直到找到最短的路径。在查找过程中对题目的条件进行实现,具体实现方式见代码注释。

代码
#include "stdio.h"
int MAX = 0x3f3f3f3f;
int map[505][505];//用于存放城市间的路径长度
int dis[505];
int flag[505];
int total[505];
int sum[505];
int weight[505];
void dijs(int a,int b,int n){
    int i,j;
    for (i = 0;i < n;i++){
        dis[i] = map[a][i];
        flag[i] = 0;
        total[i] = 0;
        sum[i] = 0;
    }//初始化距离为当前城市到各个城市的路径长度。
    sum[a] = 1;
    dis[a] = 0;
    total[a] = weight[a];//救援队总数量。
    for (i = 0;i < n;i++){
        int min = MAX;
        int x = a;
        for(j = 0;j < n;j++){
            if (!flag[j] && dis[j] < min){
                x =j;
                min = dis[j];
            }
        }//依照次序遍历城市,找到距离当前城市最近的城市。
        flag[x] = 1;//将浏览标志置为1表示已经查找过了。
        for (j = 0;j < n;j++){//从这个城市开始继续在地图上找下一个点,找到下一个路金最短的城市。
            if (!flag[j] && map[x][j] != MAX){
                if (dis[x] + map[x][j] == dis[j]){
                    sum[j] += sum[x];//如果路径相等,sum加上当前结点的总数量;
                    if (total[j] < total[x] + weight[j])
                        total[j] = total[x] + weight[j];//如果救援队数量小于新路径的数量,将当前的救援队数量替换为新的。
             }
                if (dis[x] + map[x][j] < dis[j]){
                    dis[j] = dis[x] + map[x][j];//如果路径小于当前路径,最短路径替换为当前路径。
                    total[j] = total[x] + weight[j];//救援队数量也进行替换。
                    sum[j] = sum[x];//下一个结点的路径数替换为当前结点的路径数。
                }
            }
        }
    }
}

int main(){
    int c1,c2;
    int N,M;
    int i,j;
    int t1,t2,t3;
    for (i = 0;i < 505;i++){
        for (j =0;j < 505;j++){
            if (i == j) map[i][j] = 0;
            else map[i][j] = MAX;
        }
    }//将路径全部置为最大值,方便进行后续的处理。
    scanf("%d%d%d%d",&N,&M,&c1,&c2);
    for (i = 0;i < N;i++)
    {
        scanf("%d",&weight[i]);//输入每个城市拥有的救援队数量。
    }
    for (i = 0;i < M;i++){
        scanf("%d%d%d",&t1,&t2,&t3);
        map[t1][t2] = t3;
        map[t2][t1] = t3;
    }//输入城市间的路径。
    dijs(c1, c2, N);//迪杰斯特拉算法找出最短路径,参数为当前所在城市,目标城市,城市数量。
    printf("%d %d\n",sum[c2],total[c2]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值