SPFA算法模板

 简单的一个模板吧,留个底。如果要判负环的话,需要加一个cnt数组记录入队次数就可以了。

void spfa(int st) {memset(dis,INF,sizeof dis);queue<int> q;q.push(st);dis[st]=0;vis[st]=1;while(!q.empty()) {int cur = q.front();q.pop();vis[cur]=0;int up = vv[cur].size();for(int i = 0; i<up; i++) {Edge now = vv[cur][i];int to = now.to;if(dis[to] > dis[cur] + now.w) {dis[to] = dis[cur]+now.w;if(vis[to] == 0) vis[to]=1,q.push(to);}}}
}

单源最短路条数:

void spfa(int st) {memset(dis,INF,sizeof dis);queue<int> q;q.push(st);dis[st]=0;vis[st]=1;while(!q.empty()) {int cur = q.front();q.pop();vis[cur]=0;int up = vv[cur].size();for(int i = 0; i<up; i++) {Edge now = vv[cur][i];int to = now.to;if(dis[to] > dis[cur] + now.w) {dis[to] = dis[cur]+now.w;dp[to] = dp[cur];if(vis[to] == 0) vis[to]=1,q.push(to);}else if(dis[to] == dis[cur] + now.w) {dp[to] += dp[cur];}}}
}

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441345.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

js和php能生成一样的随机数_JavaScript_JS生成某个范围的随机数【四种情况详解】,前言: JS没有现成的函数,能 - phpStudy...

JS生成某个范围的随机数【四种情况详解】前言&#xff1a;JS没有现成的函数&#xff0c;能够直接生成指定范围的随机数。但是它有个函数&#xff1a;Math.random() 这个函数可以生成 [0,1) 的一个随机数。利用它&#xff0c;我们就可以生成指定范围内的随机数。而涉及范围的话…

【POJ - 3347 】Kadj Squares (计算几何,思维 或 扫描线)

题干&#xff1a; In this problem, you are given a sequence S1, S2, ..., Sn of squares of different sizes. The sides of the squares are integer numbers. We locate the squares on the positive x-y quarter of the plane, such that their sides make 45 degrees w…

按钮开关java代码,Android自定义实现开关按钮代码

我们在应用中经常看到一些选择开关状态的配置文件&#xff0c;做项目的时候用的是android的Switch控件&#xff0c;但是感觉好丑的样子子个人认为还是自定义的比较好&#xff0c;先上个效果图&#xff1a;实现过程&#xff1a;1.准备开关不同状态的两张图片放入drawable中。2.x…

java创建的zip没写入权限,java中的zip创建错误

我正在使用ZipOutputStream,FileOutputStream和FileInputStream.首先我创建了一个包含一个文件的文件夹它成功创建了.然后我尝试创建zip文件.动态地,它首次正确创建文件,但第二次,第三次在打开文件时出错.Error: zip [path/././file.zip] Cannot open The process cannot acces…

【qduoj - 夏季学期创新题】最长公共子串(水题暴力枚举,不是LCS啊)

题干&#xff1a; 描述 编写一个程序&#xff0c;求两个字符串的最长公共子串。输出两个字符串的长度&#xff0c;输出他们的最长公共子串及子串长度。如果有多个最长公共子串请输出在第一个字符串中先出现的那一个。 特别注意公共子串中可能包含有空格&#xff0c;但不计回车…

向量合并 matlab,MATLAB追加向量

如果有两个行向量 r1 和 r2 这两个行向量中各有 n 和 m 个元素&#xff0c;现在创建行向量 r 并将n和m个元素都放在行向量 r 中&#xff0c;通过附加这些载体&#xff0c;编写&#xff1a;r [r1,r2]通过追加这两个向量&#xff0c;向量r2的&#xff0c;也可以建立一个矩阵R&am…

*【CodeForces - 202C 】Clear Symmetry (思维,找规律,特判)

题干&#xff1a; Consider some square matrix A with side n consisting of zeros and ones. There are nrows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. Well denote the element of the matrix wh…

matlab将模型解封装,模型保护 - MATLAB Simulink - MathWorks 中国

当您要与第三方共享模型而又不能泄露知识产权时&#xff0c;请对模型进行保护。Test your protected model by comparing it to the original model.Attach a digital signature to your protected model.Files to include in the protected model package.Specify a post-proc…

*【CodeForces - 768B】Code For 1 (分治策略,模拟二分思想,模拟线段树思想)

题干&#xff1a; Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the decease…

matlab 自适应噪声对消,基于Matlab的RLS自适应语音噪声对消系统的设计与实现

基于Matlab 的R LS 自适应语音噪声对消系统的设计与实现①肖 哲(湖南工业大学科技学院, 湖南株洲 412008)摘 要:自适应信号处理的理论和技术经过40多年的发展和完善,已逐渐成为人们常用的语音去噪技术.而Matlab 的出现又为其提供了更为方便快捷的方法来对语音信号进行消噪处…

【qduoj - 142】 多重背包(0-1背包的另类处理,dp)

题干&#xff1a; ycb的ACM进阶之路 Description ycb是个天资聪颖的孩子&#xff0c;他的梦想是成为世界上最伟大的ACMer。为此&#xff0c;他想拜附近最有威望的dalao为师。dalao为了判断他的资质&#xff0c;给他出了一个难题。dalao把他带到一个到处都是题的oj里对他说&am…

python数字类型怎么学,python的数字类型学习之数据类型

1、在python中&#xff0c;数字并不是一个真正的对象类型&#xff0c;而是一组类似类型的分类。它支持通常的数字类型&#xff0c;还能够可以通过常量直接创建数字&#xff0c;还可以处理数字表达式。2、数字常量&#xff1a;(1)整数和浮点数常量(2)16进制、8进制、2进制常量(3…

贪心算法 -- 最小延迟调度

转自&#xff1a;https://blog.csdn.net/bqw18744018044/article/details/80285414 总结&#xff1a; 首先&#xff0c;证明贪心的时候交换论证是万能的&#xff01;其次&#xff0c;这一点如果要满足&#xff0c;也就是&#xff0c;如果你要用交换论证法&#xff0c;那么首先…

php成行排列,一个php实现的生成排列的算法

function perm($s, $n, $index){if($n 0){return ;}else{$nIndex count($index); //可用的字符串下标$res array();foreach($index as $i > $v){$tmp $index;unset($tmp[$i]); //去掉当前的前缀/* 调试信息&#xff0c;便于理解echo "len $n , cur $i , index:\n&q…

【CodeForces - 1051C 】Vasya and Multisets (模拟)

题干&#xff1a; Vasya has a multiset ss consisting of nn integer numbers. Vasya calls some number xxnice if it appears in the multiset exactly once. For example, multiset {1,1,2,3,3,3,4}{1,1,2,3,3,3,4} contains nice numbers 22 and 44. Vasya wants to spl…

apache2+支持php7,Ubuntu14.04下配置PHP7.0+Apache2+Mysql5.7

Apache步骤一&#xff1a;安装apacheronyaoubuntu:~$ sudo apt install apache2安装好后&#xff0c;在浏览器上输入localhost(服务器端&#xff0c;请输入你的IP地址)&#xff0c;回车就会看到&#xff1a;PHP7.0步骤二&#xff1a;Ubuntu14.04下的默认源是PHP5.0&#xff0c;…

php怎么添加验证码,PHP添加验证码以及使用

现在很多页面在使用表单提交时&#xff0c;都会使用到验证码的使用、如何制做一个验证码呢&#xff1f;这里有一个用PHP的方法 以及代码1、首先在php.ini 配置文件里面把GD库打开 // 在php.ini里面找到 extensionphp_gd2.dll 把前面的分号删去。2、代码&#xff1a;<?php …

【CodeForces - 1051D】Bicolorings (dp,类似状压dp)

题干&#xff1a; You are given a grid, consisting of 22 rows and nn columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells AA and BB be…

oracle内存锁,Oracle OCP之硬解析在共享池中获取内存锁的过程

(1)在父游标的名柄没有找到SQL语句的文本&#xff1a;select * from gyj_t1 where id1;2、释放library cache Latch3、获得shared pool Latch(1)搜索FreeList 空闲Chunk(2)搜索LRU上可覆盖的chunk(3)搜索R-FreeList 空闲Chunk(4)如果没空间了&#xff0c;直接ORA-04031错误4、释…

【CodeForces - 214B】Hometask (模拟,有坑)

题干&#xff1a; Furik loves math lessons very much, so he doesnt attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You ar…