PAT class a real problem 1003 Emergency (25 points) C + + implementation (based on Dijkstra algorithm)

subject

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

thinking

It's very difficult. It involves graph algorithm. We must first clear up the thinking, pseudo code is almost written, and then start to knock.

Based on Dijkstra algorithm, that is, to find the shortest path of a single source, first review the following algorithm ideas:

The point set V is divided into two parts, s and V-S. the initial s is empty (you can also add the starting point s to the initial set, i.e. S={s}, just set the initial value). Each step adds a node to the s.

The basis for selecting points i s that for each node i ∈ V-S, the shortest path from s to I is recorded as dist[i] (if not, dist[i] = ∞). Select the minimum dist[j] from all dist[i], and j is the selected point.

After adding j to S, for all current I ∈ V-S, investigate the distance from j to I, update dist[i]: dist[i] = min {dist [j] + E [j] [i], dist[i]}.

Cycle until V=S ends. The shortest path length to the destination t is dist[t].

On the basis of Dijkstra algorithm, we need to calculate the sum of the shortest path and the largest number of people. Path count [i] is used to represent the shortest path number from source point to node i, and num[i] is used to represent the maximum number of rescue teams that can be accumulated on the shortest path before source point to each node.

When dist[i] is updated after adding a node j, if a new shortest path is found, pathCount[i] and num[i] are updated as follows:

if (newDist<dist[i]){  //There is a new shortest path
    dist[i] = newDist;
    num[i] = num[j] + v[j]; //Update the maximum accumulated quantity to this point along the shortest path
    pathCount[i] = pathCount[j]; //Update the number of shortest paths
}
else if (newDist==dist[i]){  //Shortest path with same distance
    num[i] = max(num[i], num[j] + v[j]); //Take the larger cumulative number of the same shortest path to this point
    pathCount[i] += pathCount[j];  //Note that instead of adding one, the number of new nodes is increased
}

INT_MAX is used in the code to represent the maximum integer, which is defined in climits.h. The common constant definitions in this header file are as follows:

INT_MAX int Max
 Int min int min
 UINT_MAX unsigned int Max
 Uint min unsigned int min
 Long ABCD Max long Max
 Long min long
 FLT min float type positive minimum
 FLT_MAX float type positive maximum

The functions of max and min are defined in algorithm.h.

Code

#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;

int main(){
    int n, m, c1, c2;
    cin >> n >> m >> c1 >> c2;
    vector<int> v(n);
    vector<vector<int> > e(n, vector<int>(n, INT_MAX));
    for (int i=0; i<n; i++){
        cin >> v[i];
    }
    for (int i=0; i<m; i++){
        int j, k, len;
        cin >> j >> k >> len;
        e[j][k] = len;
        e[k][j] = len;
    }
    //Dijkstra algorithm
    vector<int> dist(n, INT_MAX);  //The shortest distance from the source point to each node
    dist[c1] = 0;
    vector<int> pathCount(n);  //Minimum number of paths from source to each node
    pathCount[c1] = 1;
    vector<int> num(n);  //The maximum number of rescue teams that can be accumulated on the shortest path before each node from the source point
    vector<bool> flag(n); //Node selected to determine shortest path
    for (int count=0; count<n; count++){ //Add 1 node at a time, end after adding all nodes
        int minDist = INT_MAX;
        int minI = -1;
        for (int i=0; i<n; i++) {
            if (!flag[i] && dist[i]<minDist) {
                minDist = dist[i];
                minI = i;
            }
        }
        flag[minI] = true;
        for (int i=0; i<n; i++){ //Consider to update the dist value of unselected nodes from the newly added nodes
            if (!flag[i] && e[minI][i]<INT_MAX){
                int newDist = minDist + e[minI][i];
                if (newDist<dist[i]){  //There is a new shortest path
                    dist[i] = newDist;
                    num[i] = num[minI] + v[minI]; //Update the maximum accumulated quantity to this point along the shortest path
                    pathCount[i] = pathCount[minI]; //Update the number of shortest paths
                }
                else if (newDist==dist[i]){  //The shortest path with the same distance
                    num[i] = max(num[i], num[minI] + v[minI]); //Take the larger cumulative number of the same shortest path to this point
                    pathCount[i] += pathCount[minI];  //Note that instead of adding one, the number of new nodes is increased
                }
            }
        }
    }
    cout << pathCount[c2] << " " << num[c2] + v[c2] << endl;
    return 0;
}

133 original articles published, 7 praised, 3195 visited
Private letter follow

Posted by jib on Mon, 20 Jan 2020 04:35:51 -0800