0%

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, $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

1
5 6 0 2
2
1 2 1 5 3
3
0 1 1
4
0 2 2
5
0 3 1
6
1 2 1
7
2 4 1
8
3 4 1

Sample Output

1
2 4

分析

题目大意为:现给定一张无向图(节点有权),求从一点出发到另一点的最短路径的条数,以及经过节点的权之和的最大值。

利用Dijkstra算法,求起点到其余点的最短路径。(1)求最短路径条数:为每个节点分配一个num表示最短路径条数,在找到最近点v更新其余点时,若以最近点v为中转点u与s距离更近则num[u]=num[v];若距离相等,则说明有多条路径,即num[u]+=num[v]。(2)求经过节点的权之和的最大值:为每个节点分配一个w表示权重之和(weight为该节点权重),在找到最近点v更新其余点时,若以最近点v为中转点u与s距离更近,则w[u]=weight[u]+w[v];若距离相等,则需要判断哪条路径权重之和更大,w[u]=max(w[u],weight[u]+w[u])

AC代码

1
#include <iostream>
2
using namespace std;
3
#define MAXV 500
4
const int INF = 0x3fffffff;
5
int dis[MAXV][MAXV], d[MAXV], num[MAXV] = { 0 }, weight[MAXV], w[MAXV] = { 0 };
6
bool vis[MAXV] = { false };
7
int n, m, st, ed;
8
void dijkstra(int s)
9
{
10
    fill(d, d + MAXV, INF);
11
    d[s] = 0;
12
    w[s] = weight[s];
13
    num[s] = 1;
14
    for (int i = 0; i < n; i++)
15
    {
16
        int v = -1, mindis = INF;
17
        for (int j = 0; j < n; j++)
18
        {
19
            if (!vis[j] && d[j] < mindis)
20
            {
21
                mindis = d[j];
22
                v = j;
23
            }
24
        }
25
        if (v == -1 || v == ed) return;
26
        vis[v] = true;
27
        for (int j = 0; j < n; j++)
28
        {
29
            if (!vis[j] && dis[v][j] != INF)
30
            {
31
                if (d[v] + dis[v][j] < d[j])
32
                {
33
                    d[j] = d[v] + dis[v][j];
34
                    num[j] = num[v];
35
                    w[j] = weight[j] + w[v];
36
                }
37
                else if (d[v] + dis[v][j] == d[j])
38
                {
39
                    num[j] += num[v];
40
                    if (weight[j] + w[v] > w[j]) w[j] = weight[j] + w[v];
41
                }
42
            }
43
        }
44
    }
45
}
46
int main()
47
{
48
    fill(dis[0], dis[0] + MAXV * MAXV, INF);
49
    scanf("%d%d%d%d", &n, &m, &st, &ed);
50
    for (int i = 0; i < n; i++) scanf("%d", &weight[i]);
51
    for (int i = 0; i < m; i++)
52
    {
53
        int s, e, l;
54
        scanf("%d%d%d", &s, &e, &l);
55
        dis[s][e] = dis[e][s] = l;
56
    }
57
    dijkstra(st);
58
    printf("%d %d", num[ed], w[ed]);
59
    return 0;
60
}

原题地址

1003 Emergency (25分)

如果您觉得这篇分享不错,可以打赏作者一包辣条哦~