码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • AcWing 383. 观光 题解(最短路)


    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
  • 相关阅读:
    CH340系列Linux驱动安装
    十四天学会C++之第一天(入门和基本语法)
    MySQL——基础50题
    Revit土建模块【图转轴网】功能快速生成轴网
    《安富莱嵌入式周报》第279期:强劲的代码片段搜索工具,卡内基梅隆大学安全可靠C编码标准,Nordic发布双频WiFi6 nRF7002芯片
    服务器启用SGX(以PowerEdge R750为例)
    【线性系统理论】笔记三
    Ubuntu切换不同版本的cuda
    【云原生之Docker实战】使用Docker部署Mango个人漫画服务器
    【C++】STL容器——vector类的使用指南(含代码演示)(11)
  • 原文地址:https://blog.csdn.net/qiaodxs/article/details/125511828
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号