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, C
1

and C
2

  • 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 c
    1

    , c
    2

    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 C
    1

    to C
    2

    .

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C
1

and C
2

, 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<iostream>
#include<algorithm>
using namespace std;

int e[510][510];  //指出地图
int dis[510];  //初始节点到i节点的最短距离
int visit[510]={0};  //该节点是否经过迪杰斯特拉算法得到了最短距离
const int inf=9999999;

int weight[510],num[510],w[510]; 
 //weight[]指出i点救援队人数 要cin
 //num[]指出初始节点到i号节点最短路径有几条
 //w[]指出到i节点的救援队最多要几个

int main()
{
    int n, m, c1, c2;
    cin>>n>>m>>c1>>c2;
    fill(e[0],e[0]+510*510,inf);
    fill(dis,dis+510,inf);
    for(int i=0;i<n;i++)
        cin>>weight[i];
    int a,b,c;
    for(int i=0;i<m;i++)
    {
        cin>>a>>b>>c;
        e[a][b]=e[b][a]=c;
    }
    
    dis[c1]=0;  //初始节点到初始节点为初始的最短距离,为0
    w[c1]=weight[c1];  //每个点最至少有他自己那个节点那么多的救援队
    num[c1]=1;  //由题,至少有一个最短路径
    
    for(int i=0;i<n;i++)
    {
        int u=-1;
        int min1=inf;
        for(int j=0;j<n;j++)
        {
            if(visit[j]==0&&dis[j]<min1)  //找出一个没有经过算法纳入并离出发节点最近的**可达节点**
            {
                u=j;
                min1=dis[j];
            }
        }
        if(u==-1) break; //不存在没有经过算法纳入的节点了,退出算法
        visit[u]=1; //将该节点纳入

        for(int v=0;v<n;v++)  //更新纳入节点的可达节点的最短路径/dis数组
        {  //条件是纳入节点的可达节点应该是未经算法纳入的节点
            if(visit[v] == false && e[u][v] != inf)
            {
                if(dis[u] + e[u][v] < dis[v]) //如果可达节点更新条件是经过纳入节点
                {
                    dis[v] = dis[u] + e[u][v]; 
                    num[v] = num[u]; //到达可达节点的路径数量应等于纳入节点
                    w[v] = w[u] + weight[v]; //救援队数量直接相加
                }
                else if(dis[u] + e[u][v] == dis[v])  //如果可达节点到出发节点的距离等于经过纳入节点到可达节点
                {
                    num[v] = num[v] + num[u]; //到可达节点的路径数目
                    if(w[u] + weight[v] > w[v]) //是否更新救援队数量
                    w[v] = w[u] + weight[v];
                }
             }
        }
    }
    cout<<num[c2]<<' '<<w[c2];
    return 0;
}

讲一下迪杰斯特拉算法
迪杰斯特拉包括三个基本数据:e[510][510],dis[510],visit[510]={0};
分别表示的意义是:
e[][]:指出地图;
dis[]:指出出发节点到每个节点的最短距离;
visit[i]:指出第i个节点是否已经过迪杰斯特拉算法找出最小值。(是1/否0)

迪杰斯特拉算法求出的是每个节点到出发节点的最短距离。算法步骤:
1.从出发节点开始,每次更新出到可达节点的路径,即更新dis[];
2.加入一个新的节点,即更新visit
3.依此法直到所有节点均选出,算法结束。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值