当前位置:网站首页>AcWing 383. 观光 题解(最短路)
AcWing 383. 观光 题解(最短路)
2022-07-02 18:27:00 【乔大先生】
AcWing 383. 观光
和找最短路方案类似,只不过多了一次次短路,需要考虑在什么情况下需要更新次短路状态,这里用二维数组表示很妙,值得学习
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 20010;
int cse;
int h[N], e[M], w[M], ne[M], idx;
int S, T, n, m;
int dist[N][2], cnt[N][2]; //第一维记录点,第二维记录是最短路还是次短路
bool st[N][2]; //第一维记录点,第二维记录是最短路还是次短路
struct Ver{
int id, type, dist;
bool operator> (const Ver &W) const{
return dist > W.dist;
}
};
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
}
int dijkstra(){
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
memset(dist, 0x3f, sizeof dist);
dist[S][0] = 0; //初始化距离值
cnt[S][0] = 1; //初始化方案数
priority_queue<Ver, vector<Ver>, greater<Ver>>heap; //大根堆
heap.push({
S, 0, 0}); //第一个点入堆
while(heap.size()){
Ver v = heap.top();
heap.pop();
int ver = v.id, type = v.type, d = v.dist, ct = cnt[ver][type];
if(st[ver][type]) continue;
st[ver][type] = true;
for(int i = h[ver]; ~i; i = ne[i]){
int j = e[i];
if(dist[j][0] > d + w[i]){
//先更新次短路的状态,直接继承最短路的状态
dist[j][1] = dist[j][0];
cnt[j][1] = cnt[j][0];
heap.push({
j, 1, dist[j][1]});
//更新最短路的状态
dist[j][0] = d + w[i];
cnt[j][0] = ct;
heap.push({
j, 0, dist[j][0]});
}
else if(dist[j][0] == d + w[i]){
cnt[j][0] += ct;
}
else if(dist[j][1] > d + w[i]){
//找到新的次短路
dist[j][1] = d + w[i];
cnt[j][1] = ct;
heap.push({
j, 1, dist[j][1]}); //找到新的点状态之后记得入队
}
else if(dist[j][1] == d + w[i]){
cnt[j][1] += ct;
}
}
}
int res = cnt[T][0];
if(dist[T][0] + 1 == dist[T][1]) res += cnt[T][1];
return res;
}
int main()
{
cin>>cse;
while(cse -- ){
cin>>n>>m;
memset(h, -1, sizeof h);
idx = 0;
while(m -- ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, c);
}
cin>>S>>T;
cout<<dijkstra()<<endl;
}
return 0;
}
边栏推荐
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- 机器学习笔记 - 时间序列预测研究:法国香槟的月销量
- PyTorch函数中的__call__和forward函数
- 2022.7.1-----leetcode.241
- Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- Memory management of C
- High frequency interview questions
- mysql备份后缀是什么_mysql备份还原
- 高频面试题
猜你喜欢

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
![[paper reading] Ca net: leveraging contextual features for lung cancer prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[paper reading] Ca net: leveraging contextual features for lung cancer prediction

高频面试题

End to end object detection with transformers (Detr) paper reading and understanding

Compile oglpg-9th-edition source code with clion

Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5

End-to-End Object Detection with Transformers(DETR)论文阅读与理解
随机推荐
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
Emmet basic syntax
新手必看,点击两个按钮切换至不同的内容
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
Binary operation
Getting started with typescript
冒泡排序数组
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
【ERP软件】ERP体系二次开发有哪些危险?
Learn the knowledge points of eight part essay ~ ~ 1
线程应用实例
Golang:[]byte to string
In pytorch function__ call__ And forward functions
How performance testing creates business value
搭建哨兵模式reids、redis从节点脱离哨兵集群
预处理和预处理宏
股票证券公司排名,有安全保障吗
《重构:改善既有代码的设计》读书笔记(下)
Mobile robot path planning: artificial potential field method [easy to understand]
Talk about the design of red envelope activities in e-commerce system