(C语言)djikstra算法 PAT (Advanced Level) Practice 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.

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

#include<stdio.h>
#include<stdlib.h>

#define PATH 1000
#define MAX_CITY 10000
#define INF 0XFFFFFFF
#define true 1
#define false 0

int main(){
    int numOfCity,numOfPath,start,end;
    int resNum[MAX_CITY] = {0};
    int a[PATH][3];//图
    
    int visited[MAX_CITY] = {0};//记录走到第几个的城市
    int maxRes[MAX_CITY] = {0};//记录第几个城市的救援队
    int lenToICity[MAX_CITY] = {0};//记录到城市的最小长度
    int numOfshortestPath[MAX_CITY] = {0};
    int curMincity;
    scanf("%d %d %d %d",&numOfCity,&numOfPath,&start,&end);
    for(int i = 0;i<numOfCity;i++)
        scanf("%d",&resNum[i]);
    for(int i = 0;i<numOfPath;i++)
        scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);
    //init
    visited[start] = true;//初始化为在起点
    for(int i = 0;i < numOfCity;i++)
        lenToICity[i] = INF;//距离都初始化为无限大
    for(int i = 0;i < numOfPath;i++){//初始化起点附近
        if(a[i][0]==start){
            lenToICity[a[i][1]] = a[i][2];//到该城市的距离
            maxRes[a[i][1]] = resNum[start] +resNum[a[i][1]];//救援队为起点+该城市
            numOfshortestPath[a[i][1]]++;//到这个城市的最短路径加一
        }
        else if(a[i][1]==start){
            lenToICity[a[i][0]] = a[i][2];//到该城市的距离
            maxRes[a[i][0]] = resNum[start] +resNum[a[i][0]];//救援队为起点+该城市
            numOfshortestPath[a[i][0]]++;//到这个城市的最短路径加一
        }
    }
    while(1){
        curMincity = start;
        for(int i = 0;i<numOfCity;i++){//寻找最近的城市
            if(visited[i]==false&&lenToICity[i]!=0&&lenToICity[i]<lenToICity[curMincity]){
                curMincity = i;
            }
        }
        if(curMincity == start|| curMincity == end)
            break;//所有城市都进过了
        visited[curMincity] = true;
        for(int i = 0;i < numOfPath;i++){
            if(a[i][0]==curMincity){
                if(visited[a[i][1]]==false){
                    if(lenToICity[a[i][1]]>(lenToICity[curMincity]+a[i][2])){//如果距离是无穷大,则更新距离
                        lenToICity[a[i][1]] = (lenToICity[curMincity]+a[i][2]);
                        maxRes[a[i][1]] = maxRes[curMincity] + resNum[a[i][1]];
                        numOfshortestPath[a[i][1]] = numOfshortestPath[curMincity];//到a[i][1]的最短路径数量等于到现在城市的最短路径数量
                    }
                    else if(lenToICity[a[i][1]]==(lenToICity[curMincity]+a[i][2])){//如果该城市距离不是无穷大,即和之前的城市有连接且距离相等
                        numOfshortestPath[a[i][1]] += numOfshortestPath[curMincity];//最短路径数量等于两者之和
                        if(maxRes[a[i][1]]<maxRes[curMincity]+resNum[a[i][1]])//是否之前到达该城市的救援队数量少于现在到达该城市方法的救援队
                            maxRes[a[i][1]] = maxRes[curMincity]+resNum[a[i][1]];
                    }
                }
            }
            else if(a[i][1]==curMincity){
                if(visited[a[i][0]]==false){
                    if(lenToICity[a[i][0]]>(lenToICity[curMincity]+a[i][2])){//如果距离是无穷大,则更新距离
                        lenToICity[a[i][0]] = (lenToICity[curMincity]+a[i][2]);
                        maxRes[a[i][0]] = maxRes[curMincity] + resNum[a[i][0]];
                        numOfshortestPath[a[i][0]] = numOfshortestPath[curMincity];//到a[i][0]的最短路径数量等于到现在城市的最短路径数量
                    }
                    else if(lenToICity[a[i][0]]==(lenToICity[curMincity]+a[i][2])){//如果该城市距离不是无穷大,即和之前的城市有连接且距离相等
                        numOfshortestPath[a[i][0]] += numOfshortestPath[curMincity];//最短路径数量等于两者之和
                        if(maxRes[a[i][0]]<maxRes[curMincity]+resNum[a[i][0]])//是否之前到达该城市的救援队数量少于现在到达该城市方法的救援队
                            maxRes[a[i][0]] = maxRes[curMincity]+resNum[a[i][0]];
                    }
                }
            }
        }
       
    }
    numOfshortestPath[start] = 1;//从起点到起点最短路径是1
    maxRes[start] = resNum[start];//从起点到起点最多救援队是起点救援队
    printf("%d %d", numOfshortestPath[end], maxRes[end]);
    
    
}

版权声明:本文为weixin_43857778原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:PAT真题(C语言)——1003:Emergency_BakerIsMe的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值