1003 Emergency(25分)(Dijkstra)

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 N1), 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 c1c2 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
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int n,m,c1,c2;
 5     int ca,cb;
 6     int team[505]; //救援队人数
 7     int map[1005][1005]={0}; //带权邻接矩阵
 8     int dist[1005]; //到此点时最短距离
 9     int flag[1005]={0}; //标记该点是否已完成最短路径计算
10     int amount[1005]={0}; //到此点时最优救援人数
11     int count[1005]={0}; //到此点时最短路径数
12     cin>>n>>m>>c1>>c2;
13     for (int i=0;i<n;++i){
14         cin>>team[i];
15     }
16     //初始化邻接矩阵
17     for (int i=0;i<m;++i){
18         cin>>ca>>cb;
19         cin>>map[ca][cb];
20         map[cb][ca]=map[ca][cb];
21     }
22     //状态初始化
23     amount[c1]=team[c1];
24     count[c1]=1;
25     memset(dist,0x7f,sizeof(dist));
26     dist[c1]=0;
27     //dijkstra
28     while(flag[c2]!=1){
29         int temp=0x3f3f3f3f;
30         int point;
31         for (int i=0;i<n;++i){
32             if (flag[i]==0&&dist[i]<temp){
33                 point=i;
34                 temp=dist[i];
35             }
36         }
37        flag[point]=1;
38         for (int i=0;i<n;++i){
39             if (map[point][i]>0&&flag[i]==0){
40                 if (dist[i]>dist[point]+map[point][i]){
41                     dist[i]=dist[point]+map[point][i];
42                     amount[i]=amount[point]+team[i];
43                     count[i]=count[point];
44                 }
45                 else if (dist[i]==dist[point]+map[point][i]){
46                     count[i]+=count[point];
47                     amount[i]=max(amount[i],amount[point]+team[i]);
48                 }
49             }
50         }
51     }
52     cout<<count[c2]<<" "<<amount[c2];
53 }

 

标准的单源最短路径问题,其中共有三个数组的数据需要维护

1.当前最短距离数组(dist),使用Dijkstra算法维护,每次循环确定一个结点数据

2.当前最大救援队人数数组(amount),在更新最短距离数组时若出现同距离的情况,则选取人数更多的结点进行更新,否则正常更新

3.当前最短路径数组(count),在更新最短距离数组时若出现同距离的情况,说明该点的最短路径存在多条,需要把目前该结点已有的路径数与上一被确定结点的已有的路径数相加来作为当前结点的路径数,否则正常继承上一被确定结点的路径数

前两个数组都比较好理解,第三个数组我一开始维护的有问题,没有考虑到他和之前的结点的联系,只是单纯的在之前结点的基础上进行+1,挂了一半的测试点。

posted on 2022-08-08 17:59  Coder何  阅读(45)  评论(0)    收藏  举报