当前位置:网站首页>L2-001 紧急救援 (25 分)
L2-001 紧急救援 (25 分)
2022-07-06 09:14:00 【%xiao Q】
题意:
要求你求出最短路径的数量和最多的救援队的数量,并求S到D的的路径(该路径就是路径最短且召集救援队数量最多的路径)种经过的城市?
分析:
显而易见,这题要用dijkstra算法,不会该算法的可以去学一下,这里的难点在于如何求最短路径的数量和经过的城市,我们可以用三个数组存最短路径的数量,召集救援队的数量,这个点的前驱,然后在更新其它点的时候更新这些数组吗,详情请看代码。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 510;
int n, m, s, d;
int g[N][N];
bool st[N];
int cnt[N], number[N]; // 分别用来存救援队的数量和最短路径的数量
int path[N]; // 记录一个点的前驱
int dist[N], w[N];
void dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
cnt[s] = w[s]; //起始点的救援队数量
number[s] = 1; //一开始最短路径为1
// cout << dist[s] << ' ' << cnt[s] << ' ' << number[s] << endl;
rep(i, 1, n)
{
int t = -1;
rep(j, 0, n - 1)
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
st[t] = true;
rep(j, 0, n - 1)
{
if(dist[j] > dist[t] + g[t][j])
{
dist[j] = dist[t] + g[t][j]; //更新最短路径
cnt[j] = cnt[t] + w[j]; //更新救援队的数量
path[j] = t; //记录前驱
number[j] = number[t]; //更新最短路径的数量,因为这里只能从 t->j,即最短路径数量并不会改变
}
else if(dist[j] == dist[t] + g[t][j])
{
if(cnt[j] < cnt[t] + w[j]) // 考虑在路径相等的情况下,救援队的数量
{
cnt[j] = cnt[t] + w[j];
path[j] = t;
}
number[j] += number[t]; //因为是t->j,加上t的最短路径的数量就是j的最短路径的数量
}
}
}
}
int main()
{
cin >> n >> m >> s >> d;
rep(i, 0, n - 1) cin >> w[i];
rep(i, 0, n - 1) path[i] = i;
memset(g, 0x3f, sizeof g);
rep(i, 1, m)
{
int x, y, z;
cin >> x >> y >> z;
g[x][y] = z;
g[y][x] = z;
}
dijkstra();
cout << number[d] << ' ' << cnt[d] << "\n";
vector<int> ans;
int p = d;
while(1) //倒推路径
{
if(p == path[p])
{
ans.push_back(p);
break;
}
ans.push_back(p);
p = path[p];
}
pre(i, 0, ans.size() - 1) //逆序输出
{
if(i != 0) cout << ans[i] << ' ';
else cout << ans[i] << "\n";
}
return 0;
}
边栏推荐
- 记一次某公司面试题:合并有序数组
- Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
- Armv8-a programming guide MMU (2)
- In the era of DFI dividends, can TGP become a new benchmark for future DFI?
- Invalid global search in idea/pychar, etc. (win10)
- How to configure flymcu (STM32 serial port download software) is shown in super detail
- 连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
- Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
- Learning question 1:127.0.0.1 refused our visit
- AcWing 1298. Solution to Cao Chong's pig raising problem
猜你喜欢

When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page

Postman environment variable settings

Software testing and quality learning notes 3 -- white box testing

连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法

Pytorch基础

Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution

软件测试与质量学习笔记3--白盒测试

Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
![[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)](/img/b7/aae35f049ba659326536904ab089cb.png)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
随机推荐
LeetCode #461 汉明距离
QT creator runs the Valgrind tool on external applications
SSM整合笔记通俗易懂版
QT creator create button
02-项目实战之后台员工信息管理
Windows下安装MongDB教程、Redis教程
Basic use of redis
Project practice - background employee information management (add, delete, modify, check, login and exit)
Rhcsa certification exam exercise (configured on the first host)
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
自动机器学习框架介绍与使用(flaml、h2o)
Invalid global search in idea/pychar, etc. (win10)
When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
AcWing 1298. Solution to Cao Chong's pig raising problem
Ansible实战系列二 _ Playbook入门
Postman environment variable settings