Emergency (25分)

该博客探讨了一种紧急救援团队领导者如何利用特殊地图快速规划从当前城市到目标城市的最短路径,并在途中最大限度地召集救援队伍。输入包括城市数量、道路数量、起点和终点城市,以及各城市的救援队伍数量和道路长度。输出是最短路径的数量和能聚集的最大救援队伍数。算法基于Dijkstra's算法进行路径搜索,同时考虑了路径条数和资源调度的双重指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 444 positive integers: N(≤500)N (≤500)N(500) - the number of cities (and the cities are numbered from 000 to N−1N−1N1), MMM - the number of roads, C​1C_​1C1and C​2C_​2C2- the cities that you are currently in and that you must save, respectively. The next line contains NNN integers, where the i−thi-thith integer is the number of rescue teams in the i−thi-thith city. Then MMM lines follow, each describes a road with three integers c1c_1c1,c2c_2c2 and LLL, 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 C1C1C1to C2C2C2

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1C_1C1and C2C_2C2 , 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. 此题是求最短路径条数加上第二标尺点权的综合应用。
  2. d[u]+dis==d[v]d[u]+dis==d[v]d[u]+dis==d[v]时,无论mx[u]+a[v]>mx[v]mx[u]+a[v]>mx[v]mx[u]+a[v]>mx[v]是否成立,都应当让num[v]+=num[u]num[v]+=num[u]num[v]+=num[u],因为最短路径条数的依据仅仅是第一标尺距离,与点权无关。
#include <bits/stdc++.h>

using namespace std;
const int maxn = 510;
const int inf = 0x3f3f3f3f;
int n,m,s,t;
struct edge{
    int v,dis;
};
vector<edge>e[maxn];
int a[maxn];
int d[maxn];
bool vis[maxn];
int num[maxn];
int mx[maxn];

void Dijstra(int s) {
    memset(d, 0x3f, sizeof(d));
    d[s] = 0;
    num[s] = 1;
    mx[s]=a[s];
    for (int i = 0; i < n; i++) {
        int u = -1, mi = inf;
        for (int j = 0; j < n; j++) {
            if (!vis[j] && d[j] < mi) {
                mi = d[j];
                u = j;
            }
        }
        if (u == -1) break;
        vis[u] = 1;
        for (auto &[v, dis]:e[u]) {
            if (vis[v]) continue;
            if (d[u] + dis < d[v]) {
                d[v] = d[u] + dis;
                num[v] = num[u];
                mx[v] = mx[u] + a[v];
            } else {
                if (d[u] + dis == d[v]) {
                    num[v] += num[u];
                    mx[v] = max(mx[u] + a[v], mx[v]);
                }
            }
        }
    }
    printf("%d %d\n", num[t], mx[t]);
}
int main() {
    scanf("%d%d%d%d", &n, &m, &s, &t);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        e[u].push_back({v, w});
        e[v].push_back({u, w});
    }
    Dijstra(s);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值