PAT (Advanced Level)_1003 Emergency_dijkstra

发布时间:2024年01月10日

题目

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

最短路径问题——dijkstra算法

同天梯赛L2—001

AC代码

//最短路径dijkstra 同天梯赛L2-001
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 501;   //最大结点数
//三个数组
int dist[N];         //dist[i]表示结点i到起点的距离
//int path[N];       //path[i]表示结点i的前驱,这题不用输出最短路径,用不到
bool st[N] = {0};    //st[i]表示该结点是否确定了最小距离,1是确定,0是未确定
int g[N][N];         //邻接矩阵

int num[N] = {0};    //最短路径数
int bd[N] = {0};     //救援队数
int maxbd[N] = {0};  //最大救援队数

int n,m,c1,c2;       //结点数,边数,起点,终点

void dijkstra();
int main()
{
    cin>>n>>m>>c1>>c2;        
    for(int i = 0; i<n; i++)cin>>bd[i];

    memset(g,0x3f,sizeof g);      //初始化为无穷大
    while(m--){
        int x,y,z;
        cin>>x>>y>>z;
        g[x][y] = min(g[x][y],z);
        g[y][x] = g[x][y];        //无向图
    }

    dijkstra();
    cout<<num[c2]<<" "<<maxbd[c2];

    return 0;
}
void dijkstra()
{
    memset(dist,0x3f,sizeof dist);    //把距离初始化为正无穷
    dist[c1] = 0;
    num[c1] = 1;
    maxbd[c1] = bd[c1];

    int iter = n;
    while(iter--){    //n个点,循环n次
        int t = -1;
        //t随便初始化了一个不存在的结点,它最终用来存储未确定最小距离的结点,且该结点与其它结点相比目前到起点的距离最小
        for(int i = 0; i<n; i++){
            if(st[i] == 0 && (t == -1 || dist[t]>dist[i]))t=i;
        }
        st[t] = 1;
        //用结点t依次取更新其它结点到起点的距离,最短路径条数,最大救援队数;
        for(int i = 0; i<n; i++){
            if(st[i] == 0 && (dist[t] + g[t][i])<dist[i]){
                dist[i] = dist[t] + g[t][i];
                num[i] = num[t];
                maxbd[i] = maxbd[t] + bd[i];
            }else if(st[i] == 0 && (dist[t] + g[t][i])==dist[i]){
                num[i] += num[t];
                maxbd[i] = max(maxbd[i],maxbd[t] + bd[i]);
            }
        }
    }
    
}

?dijkstra算法详解icon-default.png?t=N7T8http://t.csdnimg.cn/eJdll

文章来源:https://blog.csdn.net/WXH_111/article/details/135491240
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。