admin管理员组

文章数量:1577829

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算法,参考了柳神的题解,和一般的Dijkstra比起来多了一个要计算最短路径的个数的步骤,同时在路径一样短时结合节点的权重(救援队伍数量)取多的那个。

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
int n,m,c1,c2;//城市数,路数,我在的城市,待救援城市
int e[510][510];//任意两点之间的距离
int weight[510];//每个点的队伍数量,即权重
int dis[510];//出发点到每个点的最短路径
int num[510];//从出发点到节点i的最短路径的条数
int w[510];//从出发点到i点的队伍数量之和
bool visit[510]={false};//该点是否被访问过
const int inf=1000000000;
int main()
{
    cin>>n>>m>>c1>>c2;
    for(int i=0;i<n;i++)
        cin>>weight[i];//每个城市的队伍数量
    fill(e[0],e[0]+510*510,inf);
    fill(dis,dis+510,inf);
    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;
    w[c1]=weight[c1];
    num[c1]=1;
    for(int i=0;i<n;i++)
    {
        int u=-1,minn=inf;//u为当前访问的节点
        for(int j=0;j<n;j++)
        {
            if(visit[j]==false&&dis[j]<minn){//每次从未访问的点中找到距离起始点最短的
                u=j;
                minn=dis[j];
            }
        }
        if(u==-1)break;//没有找到新的更好的点
        visit[u]=true;
        for(int v=0;v<n;v++)
        {
            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[u];
                    if(w[u]+weight[v]>w[v])
                        w[v]=w[u]+weight[v];
                }
            }
        }
    }
    cout<<num[c2]<<" "<<w[c2];
    return 0;
}

运行结果如下:

本文标签: EmergencyDijkstraPAT