1003 Emergency
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 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, 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 Specification:
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算法可以很容易找出最短路径。
-
定义一个num[]数组——数组元素表示从开始结点到达下标结点的最短路径条数,在dijkstra算法的过程中,每当遇到路径权值相同时更新该数组。最后num[end]就是到达目标结点最短路径的条数
-
定义一个w[]数组——数组元素表示从开始结点到达下标结点的最短路径上积累的点权和,在dijkstra算法中,每当遇到相同权值的路径时,比较它们积累的点权和,其中最大的存到w[]中。
参考代码
1 #include<iostream> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 6 #define MAXV 510 7 #define INF 1000000000 8 9 //d--从源点到达下表顶点的最小路径权值 10 //w--到达下标结点最大的点权 11 //num--从源点到达下表顶点最小路径的条数 12 int d[MAXV],w[MAXV],num[MAXV],weight[MAXV]; 13 int n,m,st,ed,G[MAXV][MAXV]; 14 bool vis[MAXV]={false}; 15 16 void Dijkstra(int s) 17 { 18 fill(d,d+MAXV,INF); 19 memset(num,0,sizeof(num)); 20 memset(w,0,sizeof(w)); 21 d[s]=0; 22 w[s]=weight[s]; 23 num[s]=1; 24 for(int i=0;i<n;i++) 25 { 26 int u=-1,MIN=INF; 27 for(int j=0;j<n;j++) 28 { 29 if(vis[j]==false&&d[j]<MIN) 30 { 31 u=j; 32 MIN=d[j]; 33 } 34 } 35 if(u==-1){ 36 return; 37 } 38 vis[u]=true; 39 for(int v=0;v<n;v++) 40 { 41 //此循环不设vis[v]=true是为了让下一次的u能够找到最近未访问的结点 42 if(vis[v]==false&&G[u][v]!=INF) 43 { 44 if(d[u]+G[u][v]<d[v]){ 45 d[v]=d[u]+G[u][v]; 46 w[v]=w[u]+weight[v]; 47 num[v]=num[u]; 48 } else if(d[u]+G[u][v]==d[v]) { 49 if(w[u]+weight[v]>w[v]){ 50 w[v]=w[u]+weight[v]; 51 } 52 num[v]=num[v]+num[u]; 53 } 54 } 55 } 56 } 57 } 58 59 60 int main() 61 { 62 cin>>n>>m>>st>>ed; 63 for(int i=0;i<n;i++) 64 { 65 cin>>weight[i]; 66 } 67 fill(G[0],G[0]+MAXV*MAXV,INF); 68 for(int i=0;i<m;i++) 69 { 70 int C1,C2,L; 71 cin>>C1>>C2>>L; 72 G[C1][C2]=L; 73 G[C2][C1]=L; 74 } 75 Dijkstra(st); 76 cout<<num[ed]<<" "<<w[ed]; 77 }
总结

浙公网安备 33010602011771号