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
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
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 const int INF = 0x7fffffff; 7 struct Node 8 { 9 bool isknown = false; 10 int minLength = INF; 11 }; 12 13 int rescueTeams[500]; 14 int cityMap[500][500]; 15 int MaxTeams[500]; 16 int minPath[500]; 17 Node nodes[500]; 18 19 int main() 20 { 21 int cityNum, roadNum, start, destination; 22 23 for (int i = 0; i<500; i++) //initialize the city map 24 for (int j = 0; j<500; j++) 25 cityMap[i][j] = INF; 26 27 cin >> cityNum >> roadNum >> start >> destination; 28 for (int i = 0; i<cityNum; i++) 29 cin >> rescueTeams[i]; 30 31 //construct the city map 32 for (int i = 0; i<roadNum; i++) 33 { 34 int c1, c2, length; 35 cin >> c1 >> c2 >> length; 36 if (cityMap[c1][c2] > length) 37 cityMap[c1][c2] = cityMap[c2][c1] = length; 38 } 39 40 //using dijkstra algorithms 41 nodes[start].isknown = true; //set the start konwn 42 nodes[start].minLength = 0; 43 MaxTeams[start] = rescueTeams[start]; 44 minPath[start] = 1; 45 int minNode = start; 46 int cnt = 1; 47 while (cnt < cityNum) 48 { 49 //update the node connnected to the minimum node 50 for (int j = 0; j<cityNum; j++) 51 { 52 if (cityMap[minNode][j] < INF) 53 { 54 //with a equal length and record the route 55 if (nodes[minNode].minLength + cityMap[minNode][j] == nodes[j].minLength) 56 { 57 if (MaxTeams[j] < MaxTeams[minNode] + rescueTeams[j]) 58 MaxTeams[j] = MaxTeams[minNode] + rescueTeams[j]; 59 minPath[j] += minPath[minNode]; 60 } 61 //with a smaller length 62 else if (nodes[minNode].minLength + cityMap[minNode][j] < nodes[j].minLength) 63 { 64 nodes[j].minLength = nodes[minNode].minLength + cityMap[minNode][j]; 65 MaxTeams[j] = rescueTeams[j] + MaxTeams[minNode]; 66 minPath[j] = minPath[minNode]; 67 } 68 } 69 } 70 71 //find the unkonwn node with the minimum length 72 int minLength = INF; 73 for (int i = 0; i<cityNum; i++) 74 { 75 if (!nodes[i].isknown && minLength > nodes[i].minLength) 76 { 77 minLength = nodes[i].minLength; 78 minNode = i; 79 } 80 } 81 nodes[minNode].isknown = true; 82 cnt++; 83 } 84 85 cout << minPath[destination] << " " << MaxTeams[destination]; 86 }