☀L2-001 Emergency rescue (25 points) [PTA] [dijstra shortest path] [number of shortest paths with weight]

As the head of a city’s emergency rescue team, you have a special map of the country. On the map, there are multiple scattered cities and some express roads connecting the cities. The number of rescue teams in each city and the length of each expressway connecting the two cities are marked on the map. When other cities have emergency calls to you, your task is to lead your rescue team as soon as possible to the place where the incident occurred, and at the same time, gather as many rescue teams as possible along the way.

Input format:

Enter the first line to give 4 positive integers N, M, S, D, where N (2≤N≤500) is the number of cities. By the way, suppose the number of the city is 0 ~ (N−1); M is fast The number of roads; S is the city number of the departure place; D is the city number of the destination.

The second line gives N positive integers, where the ith number is the number of rescue teams in the ith city, and the numbers are separated by spaces. In the subsequent M lines, each line gives information about an express road, namely: city 1, city 2, and the length of the express road, separated by spaces, and the numbers are all integers and do not exceed 500. The input ensures that the rescue is feasible and the optimal solution is unique.

Output format:

The first line outputs the number of shortest paths and the maximum number of rescue teams that can be assembled. The second line outputs the city numbers that pass through the path from S to D. The numbers are separated by spaces, and no extra spaces are allowed at the end of the output.

Input sample:

4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2

Sample output:

2 60
0 1 3

 

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f;
priority_queue<int,vector<int>,greater<int>>q;
 
const int mod=10e9+7;

const int maxn=550;

int n,m,st,nd;
int vis[maxn],G[maxn][maxn],weight[maxn];
int d[maxn],w[maxn],num[maxn],pre[maxn];

void dijstra(int s)
{
    memset(d,INF,sizeof(d));
    memset(num,0,sizeof(num));
    memset(w,0,sizeof(w));
    memset(vis,0,sizeof(vis));
    d[s]=0;
    w[s]=weight[s];
    num[s]=1;
    for(int i=0;i<n;i++)
    {
        int u=-1,minn=INF;
        for(int j=0;j<n;j++)
        {
            if(vis[j]==0&&d[j]<minn)
            {
                u=j;
                minn=d[j];
            }
        }
        if(u==-1)
            return;
        vis[u]=1;
        for(int j=0;j<n;j++)
        {
            if(vis[j]==0&&G[u][j]!=INF)
            {
                if(d[u]+G[u][j]<d[j])
                {
                    d[j]=d[u]+G[u][j];
                    w[j]=w[u]+weight[j];
                    num[j]=num[u];
                    pre[j]=u;
                }
                else if(d[u]+G[u][j]==d[j])
                {
                    if(w[u]+weight[j]>w[j])
                    {
                        w[j]=w[u]+weight[j];
                        pre[j]=u;
                    }
                    num[j]+=num[u];
                }
            }
        }
    }
}
void outpath(int v)
{
    if(v==st)
    {
        cout<<v;
        return;
    }
    outpath(pre[v]);
    cout<<" "<<v;
}

int main()
{
    cin>>n>>m>>st>>nd;
    for(int i=0;i<n;i++)
    {
        cin>>weight[i];
    }
    fill(G[0],G[0]+maxn*maxn,INF);
    for(int i=0;i<m;i++)
    {
        int u,v;
        cin>>u>>v;
        cin>>G[u][v];
        G[v][u]=G[u][v];
    }
    dijstra(st);
    cout<<num[nd]<<" "<<w[nd]<<endl;
    outpath(nd);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43660826/article/details/109991013