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, C?1?? and C?2?? - 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 c?1??, c?2?? 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 C?1?? to C?2??.
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C?1?? and C?2??, 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<cstring> 3 4 using namespace std; 5 6 static const int INFTY = (1<<21); 7 static const int MAX = 800; 8 static const int WHITE = 0; 9 static const int GRAY = 1; 10 static const int BLACK = 2; 11 12 int n, M[MAX][MAX], Team[MAX]; 13 int w[MAX], num[MAX]; 14 15 void dijsktra(int source){ 16 int d[MAX]; 17 int color[MAX]; 18 for(int i=0;i<n;i++){ 19 d[i] = INFTY; 20 color[MAX] = WHITE; 21 } 22 memset(w, 0, sizeof(w)); 23 memset(num, 0, sizeof(num)); 24 d[source] = 0; 25 color[source] = GRAY; 26 num[source] = 1; 27 w[source] = Team[source]; 28 while(true){ 29 int minv = INFTY; 30 int u = -1; 31 for(int i=0;i<n;i++){ 32 if(minv>d[i]&&color[i]!=BLACK){ 33 u = i; 34 minv = d[i]; 35 } 36 } 37 if(u==-1){ 38 break; 39 } 40 color[u] = BLACK; 41 for(int v=0;v<n;v++){ 42 if(color[v]!=BLACK&&M[u][v]!=INFTY){ 43 if(d[v]>d[u]+M[u][v]){ 44 d[v] = d[u] + M[u][v]; 45 color[v] = GRAY; 46 w[v] = w[u] + Team[v]; 47 num[v] = num[u]; 48 } 49 else if(d[v]==d[u]+M[u][v]){ 50 if(w[v]<w[u] + Team[v]){ 51 w[v] = w[u] + Team[v]; 52 } 53 num[v] += num[u]; 54 } 55 } 56 } 57 } 58 } 59 60 int main(){ 61 int road, source, target; 62 cin>>n>>road>>source>>target; 63 for(int i=0;i<n;i++){ 64 for(int j=0;j<n;j++){ 65 M[i][j] = INFTY; 66 } 67 } 68 int cnt; 69 for(int i=0;i<n;i++){ 70 cin>>cnt; 71 Team[i] = cnt; 72 } 73 int u, v, cost; 74 for(int i=0;i<road;i++){ 75 cin>>u>>v>>cost; 76 M[v][u] = M[u][v] = cost; 77 } 78 dijsktra(source); 79 cout<<num[target]<<" "<<w[target]<<endl; 80 return 0; 81 }
需要注意的是MAX的设定,很奇怪的是,题目给定最大是500,设定五百会有一些测试点过不去,得开的稍微大一点。还有就是,数组需要进行初始化。
原文地址:https://www.cnblogs.com/sgatbl/p/9602053.html