当前位置:网站首页>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;
}
边栏推荐
- QT creator shape
- 01项目需求分析 (点餐系统)
- NPM an error NPM err code enoent NPM err syscall open
- Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
- Introduction and use of automatic machine learning framework (flaml, H2O)
- FRP intranet penetration
- Use dapr to shorten software development cycle and improve production efficiency
- Neo4j installation tutorial
- 打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
- Picture coloring project - deoldify
猜你喜欢
How to configure flymcu (STM32 serial port download software) is shown in super detail
When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
02-项目实战之后台员工信息管理
Why is MySQL still slow to query when indexing is used?
One click extraction of tables in PDF
Request object and response object analysis
Redis的基础使用
MySQL主从复制、读写分离
QT creator shape
Installation and use of MySQL under MySQL 19 Linux
随机推荐
AcWing 179.阶乘分解 题解
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
Ansible实战系列一 _ 入门
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Learning question 1:127.0.0.1 refused our visit
QT creator specifies dependencies
Classes in C #
What does usart1 mean
MySQL completely uninstalled (windows, MAC, Linux)
[recommended by bloggers] C WinForm regularly sends email (with source code)
AcWing 179. Factorial decomposition problem solution
csdn-Markdown编辑器
Pytorch基础
JDBC principle
JDBC原理
Tcp/ip protocol (UDP)
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
学习问题1:127.0.0.1拒绝了我们的访问
La table d'exportation Navicat génère un fichier PDM
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)