当前位置:网站首页>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;
}
边栏推荐
- Learn the knowledge points of eight part essay ~ ~ 1
- PyTorch函数中的__call__和forward函数
- 为什么要做企业固定资产管理系统,企业如何加强固定资产管理
- PHP非对称加密方法私钥及公钥加密解密的方法
- 聊聊电商系统中红包活动设计
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- 思考变量引起的巨大变化
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- 2022 software engineering final exam recall Edition
- Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
猜你喜欢

线程应用实例

Compile oglpg-9th-edition source code with clion

消息队列消息丢失和消息重复发送的处理策略

Machine learning notes - time series prediction research: monthly sales of French champagne

开发固定资产管理系统,开发固定资产管理系统用什么语音

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

Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出

Quanzhi A33 uses mainline u-boot

性能测试如何创造业务价值

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
随机推荐
[pytorch learning notes] tensor
4274. 后缀表达式-二叉表达式树
QT中的QPropertyAnimation使用和toast案列
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Virtual machine initialization script, virtual machine mutual secret key free
Codeworks 5 questions per day (1700 average) - day 4
仿京东放大镜效果(pink老师版)
程序猿入门攻略(十二)——数据的存储
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
数据降维——因子分析
PyTorch函数中的__call__和forward函数
2022 compilation principle final examination recall Edition
Memory management of C
MySQL advanced (Advanced) SQL statement
2022.7.1-----leetcode. two hundred and forty-one
数据降维——主成分分析
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
golang:[]byte转string
思维意识转变是施工企业数字化转型成败的关键
Npoi export Excel2007