PAT1003

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

主要会写Dijkstra就好了!

 1 /*用Dijkstra算最短路径,稍作修改,利用一个数组保存到当前结点的最短路径数,
 2 用另外一个数组保存从原点到当前节点最大救援队数,关键在于Dijkstra*/
 3 #include<iostream>
 4 #include<vector>
 5 #include<set>
 6 #include<iterator>
 7 using namespace std;
 8 #define M 999999999
 9 
10 void Dijkstra_version(vector<vector<int>> &map, int start, int end, 
11                                     vector<int> &path_num, vector<int> &max_rescue, 
12                                     vector<int> &dis, vector<int> &fireman)
13 {
14     dis[start] = 0;
15     path_num[start] = 1;
16     max_rescue[start] = fireman[start];
17     vector<int> used(dis.size(), 0);
18     while(start != end)
19     {
20         int min = M;
21         int kk = -1;
22         for(int i=0; i < used.size(); ++i)
23             if(used[i] == 0 && dis[i] < min)
24             {
25                 min = dis[i]; kk = i;
26             }
27         used[kk] = 1;     
28         for(int i=0; i < used.size(); ++i)
29         if(used[i] == 0)
30             if(map[kk][i] + dis[kk] < dis[i])
31             {
32                 dis[i] = map[kk][i] + dis[kk];
33                 path_num[i] = path_num[kk];
34                 max_rescue[i] = max_rescue[kk] + fireman[i];
35             }
36             else if(map[kk][i] + dis[kk] == dis[i] )
37             {
38                 path_num[i] += path_num[kk];
39                 if(max_rescue[kk] + fireman[i] > max_rescue[i])
40                     max_rescue[i] = max_rescue[kk] + fireman[i];
41             }
42         start = kk;
43     }
44 }
45 
46 int main()
47 {
48     int city_num, road_num, start, end;
49     while(cin>>city_num>>road_num>>start>>end)
50     {
51         vector<int> fireman(city_num);
52         vector<int> colum(city_num, M);
53         vector<vector<int>> map(city_num, colum);
54         for(int i=0; i<city_num; ++i)
55             cin>>fireman[i];
56         for(int i=0; i<road_num; ++i)
57         {
58             int a, b, c;
59             cin>>a>>b>>c;
60             map[a][b]=map[b][a]=c;
61         }
62         vector<int> path_num(city_num, 0);
63         //记录最短路径数
64         vector<int> max_rescue(city_num, 0);
65         //记录到当前结点最大营救队数目
66         vector<int> dis(city_num, M);
67         //最短路径距离
68         Dijkstra_version(map, start, end, path_num, max_rescue, dis, fireman);
69         cout<<path_num[end]<<" "<<max_rescue[end]<<endl;
70     }
71     return 0;
72 }
原文地址:https://www.cnblogs.com/bochen-sam/p/3351442.html