A1003. Emergency

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<cstdio>
 2 #include<iostream>
 3 #include<vector>
 4 #include<algorithm>
 5 using namespace std;
 6 int G[501][501], teams[501], dst[501], visit[501] = {0}, pathNum[501] = {0}, teamSum[501] = {0};
 7 int N, M, C1, C2;
 8 const int INF = 1000000000;
 9 void dijkstra(int s){
10     pathNum[s] = 1;
11     for(int i = 0; i < N; i++){
12         dst[i] = INF;
13     }
14     dst[s] = 0;
15     teamSum[s] = teams[s];
16     for(int i = 0; i < N; i++){
17         int u = -1, minlen = INF;
18         for(int j = 0; j < N; j++){
19             if(visit[j] == 0 && dst[j] < minlen){
20                 minlen = dst[j];
21                 u = j;
22             }
23         }
24         if(u == -1)
25             return;
26         else visit[u] = 1;
27         for(int j = 0; j < N; j++){
28             if(visit[j] == 0 && G[u][j] != INF && dst[u] + G[u][j] < dst[j]){
29                 dst[j] = dst[u] + G[u][j];
30                 pathNum[j] = pathNum[u];
31                 teamSum[j] = teamSum[u] + teams[j];
32             }else if(visit[j] == 0 && G[u][j] != INF && dst[u] + G[u][j] == dst[j] && teamSum[u] + teams[j] > teamSum[j]){
33                 dst[j] = dst[u] + G[u][j];
34                 pathNum[j] += pathNum[u];
35                 teamSum[j] = teamSum[u] + teams[j];
36             }else if(visit[j] == 0 && G[u][j] != INF && dst[u] + G[u][j] == dst[j] && teamSum[u] + teams[j] <= teamSum[j]){
37                 dst[j] = dst[u] + G[u][j];
38                 pathNum[j] += pathNum[u];
39             }
40         }
41     }
42 }
43 int main(){
44     scanf("%d%d%d%d", &N, &M, &C1, &C2);
45     int temp1, temp2, temp3;
46     for(int i = 0; i < N; i++){
47         scanf("%d", &teams[i]);
48     }
49     fill(G[0], G[0] + 501*501, INF);
50     for(int i = 0; i < M; i++){
51         scanf("%d%d%d", &temp1, &temp2, &temp3);
52         G[temp1][temp2] = G[temp2][temp1] = temp3;
53     }
54     dijkstra(C1);
55     printf("%d %d", pathNum[C2], teamSum[C2]);
56     cin >> N;
57     return 0;
58 }
View Code

 

总结:

1、迪杰斯特拉求最短路径,并计算最短路径的条数。如果有多条最短路,计算出他们中的最大点权之和。

2、迪杰斯特拉伪代码:

int visit[100], G[100][100], dst[100], pathNum[100], v[100], w[100];
void dijkstra(int s){
    初始化:visit表示已经最优的点,初始全为0.
            G存储图,初始全为INF
            dst存储到源点的最短距离,初始dst[s]为0,其它为INF
            pathNum存储到源点的最短路条数,初始pathNum[s]为1,其它全为0
            v表示从源点一路累加的点权之和(应还有一个记录点权的数组),初始v[s]为自身的点权,其它全为0
            w表示从源点一路累加的边权之和,初始只有w[s]为0,其它全为INF
    for(int i = 0; i < N; i++){//循环N次,每次能找到一个
        int u = -1;
        选择一个未被最优化的且dst最短的节点为u
        将其visit设为1
        for(int j = 0; j < N; j++){
            if(visit[j] == 0 && G[u][j] != INF){
                if(dst[u] + G[u][j] < dst[j]){
                    dst[j] = dst[u] + G[u][j];
                    pathNum[j] = pathNum[u];
                }else if(dst[u] + G[u][j] == dst[j]){
                    pathNum[j] = pathNum[j] + pathNum[u];
                }
            }
        }
    }
}

在第一标尺(最短距离)相等的情况下,第二标尺:

边权:当距离更优时,更新dst和w;当距离相等时且第二标尺更优时,更新第二标尺w。

点权:同边权

最短路径条数:当距离更优时,pathNum[ j ]继承pathNum[ u ]。当距离相等时, pathNum[ j ]累加pathNum[ u ]。

3、dijkstra + DFS:仅仅用dijkstra专心求最短路,在过程中记录前驱节点。使用 vector<int> pre[100],当 dst[u] + G[u][j] < dst[j] 时,清空pre[ j ],并将u加入其中。当 dst[u] + G[u][j] == dst[j] 时,仅仅把u加入pre[ j ]。最终得到一棵以终点C2为根,以源点C1为叶节点的树。可以使用DFS,与一个vector<int> temp,得到一条完整路径后再计算各种标尺。

4、初始化   const int INF = 100000000;       fill(G[0], G[0] + 501*501, INF);  

posted @ 2018-02-13 10:07  ZHUQW  阅读(136)  评论(0编辑  收藏  举报