简答题
偏难
#13197
城市间紧急救援 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连…
城市间紧急救援
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
时间限制:1000
内存限制:65536
输入
输入第一行给出 4 个正整数 n、m、s、d,其中 n(2 ≤ n ≤ 500)是城市的个数,顺便假设城市的编号为 0 ~ (n-1);m 是快速道路的条数;s 是出发地的城市编号;d是目的地的城市编号。 第二行给出 n个正整数,其中第 i 个数是第 i 个城市的救援队的数目,数字间以空格分隔。随后的 m 行中,每行给出一条快速道路的信息,分别是:城市 1、城市 2、快速道路的长度,中间用空格分开,数字均为整数且不超过 500。输入保证救援可行且最优解唯一。
输出
第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从 s 到 d 的路径中经过的城市编号。数字间以空格分隔。
样例输入
4 5 0 3 20 30 40 10 0 1 1 1 3 2 0 3 3 0 2 2 2 3 2
样例输出
2 60 0 1 3
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 1e9;
int main() {
int n, m, s, d;
cin >> n >> m >> s >> d;
vector<int> team(n);
for (int i = 0; i < n; ++i) {
cin >> team[i];
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; ++i) {
int u, v, len;
cin >> u >> v >> len;
adj[u].emplace_back(v, len);
}
vector<int> dist(n, INF);
vector<int> num(n, 0);
vector<int> rescue(n, 0);
vector<int> prev(n, -1);
dist[s] = 0;
num[s] = 1;
rescue[s] = team[s];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while (!pq.empty()) {
auto [cur_dist, u] = pq.top();
pq.pop();
if (cur_dist > dist[u]) continue;
for (auto [v, len] : adj[u]) {
int new_dist = dist[u] + len;
if (new_dist < dist[v]) {
dist[v] = new_dist;
num[v] = num[u];
rescue[v] = rescue[u] + team[v];
prev[v] = u;
pq.push({new_dist, v});
} else if (new_dist == dist[v]) {
num[v] += num[u];
if (rescue[u] + team[v] > rescue[v]) {
rescue[v] = rescue[u] + team[v];
prev[v] = u;
pq.push({new_dist, v});
}
}
}
}
cout << num[d] << " " << rescue[d] << endl;
vector<int> path;
int current = d;
while (current != s) {
path.push_back(current);
current = prev[current];
}
path.push_back(s);
reverse(path.begin(), path.end());
for (size_t i = 0; i < path.size(); ++i) {
if (i > 0) cout << " ";
cout << path[i];
}
cout << endl;
return 0;
}</pre>
这道题暂未补解析。