当前位置:网站首页>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;
}
边栏推荐
- 451-memcpy、memmove、memset的实现
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
- 数字滚动带动画
- Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
- 《代码整洁之道》读书笔记
- Preprocessing and preprocessing macros
- 新手必看,点击两个按钮切换至不同的内容
- Qpropertyanimation use and toast case list in QT
- juypter notebook 修改默认打开文件夹以及默认浏览器
- Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
猜你喜欢

Use cheat engine to modify money, life and stars in Kingdom rush
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

juypter notebook 修改默认打开文件夹以及默认浏览器

数据降维——主成分分析

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

新手必看,点击两个按钮切换至不同的内容

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖

高级性能测试系列《24. 通过jdbc执行sql脚本》

新手必看,點擊兩個按鈕切換至不同的內容
随机推荐
安装单机redis详细教程
虚拟机初始化脚本, 虚拟机相互免秘钥
Yunna | why use the fixed asset management system and how to enable it
《架构整洁之道》读书笔记(下)
metric_ Logger urination
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
《重构:改善既有代码的设计》读书笔记(上)
A4988驱动步进电机「建议收藏」
453-atoi函数的实现
452-strcpy、strcat、strcmp、strstr、strchr的实现
Juypter notebook modify the default open folder and default browser
Develop fixed asset management system, what voice is used to develop fixed asset management system
High frequency interview questions
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
2022 compilation principle final examination recall Edition
聊聊电商系统中红包活动设计
451-memcpy、memmove、memset的实现
MySQL
Excel finds the same value in a column, deletes the row or replaces it with a blank value