[PAT 甲级] 1003 Emergency (25 分) 【Dijkstra】

版权声明:原创博客,转载请标明出处! https://blog.csdn.net/caipengbenren/article/details/88857909

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 c

​​ 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

​​ .

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


这是一道最短路的问题,用num记录当前结点的路线数量,用save记录当前结点的救援数量。

而关于dij的实现部分,我们一般需要一个dij数组,一个book数组,一个w的二维数组记录边的长度。我们需要用INF初始化dij数组和w数组。我们需要起点的dij为0,然后循环n次。每一次都寻找没有访问的最小边,并标记这个结点,之后利用这个结点更新dij数组。注意一些break和continue即可。


在这里插入图片描述
最开心的事莫过于一次ac


#include<iostream>
using namespace std;
const int INF = 0x3fffffff;
int w[501][501];
int val[501];
int dij[501];
int save[501];
int num[501];
int book[501];
int n, m, c1, c2;
int main () {
    cin >> n >> m >> c1 >> c2;
    fill(w[0], w[0] + 501 * 501,INF);
    fill(dij, dij + 500, INF);
    for (int i = 0; i < n; i++) {
        cin >> val[i];
    }
    int a, b, c;
    for (int i = 0; i < m; i++) {
        cin >> a >> b >> c;
        w[a][b] = c;
        w[b][a] = c;
    }
    int time = n;
    dij[c1] = 0;
    save[c1] = val[c1];
    num[c1] = 1;
    while (time--) {
        int x = -1, minn = INF;
        for (int i = 0; i < n; i++) {
            if (minn > dij[i] && !book[i]) {
                x = i;
                minn = dij[i];
            }
        }
        book[x] = 1;
        if (x == c2) {
            break;
        }
        for (int i = 0; i < n;i++) {
            if (w[x][i] == INF || book[i]) {
                continue;
            }
            if (dij[i] > w[x][i] + dij[x]) {
                dij[i] = w[x][i] + dij[x];
                num[i] = num[x];
                save[i] = save[x] + val[i];
            } else if (dij[i] == w[x][i] + dij[x]) {
                num[i] += num[x];
                if(save[i] < save[x] + val[i]) {
                    save[i] = save[x] + val[i];
                }
            }
        }
    }
    cout << num[c2] << " " << save[c2];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/caipengbenren/article/details/88857909
今日推荐