码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • spfa求存在负权边的最短路


    spfa是bellman——ford的队列优化版本,通过bfs:

    优化的是dist[b] = min(dist[b], dist[a]+w)
    因为只有dist[a]更新之后变小, dist[b]更新之后才有可能变小
    需要st[i]数组,保证队列里面只有一个i;

    给定一个 nn 个点 mm 条边的有向图,图中可能存在重边和自环, 边权可能为负数。

    请你求出 11 号点到 nn 号点的最短距离,如果无法从 11 号点走到 nn 号点,则输出 impossible。

    数据保证不存在负权回路。

    输入格式

    第一行包含整数 nn 和 mm。

    接下来 mm 行每行包含三个整数 x,y,zx,y,z,表示存在一条从点 xx 到点 yy 的有向边,边长为 zz。

    输出格式

    输出一个整数,表示 11 号点到 nn 号点的最短距离。

    如果路径不存在,则输出 impossible。

    数据范围

    1≤n,m≤1051≤n,m≤105,
    图中涉及边长绝对值均不超过 1000010000。

    输入样例:

    1. 3 3
    2. 1 2 5
    3. 2 3 -3
    4. 1 3 4

    输出样例:

    2

     

    1. #include <iostream>
    2. #include <cstring>
    3. #include <queue>
    4. using namespace std;
    5. const int N = 1e05+ 10;
    6. int read(){
    7. int res = 0 , flag = 1 ;
    8. char c = getchar() ;
    9. while(!isdigit(c)){
    10. if(c == '-') flag = -1 ;
    11. c = getchar() ;
    12. }
    13. while(isdigit(c)){
    14. res = (res << 1) + (res << 3) + (c ^ 48) ;
    15. c = getchar() ;
    16. }
    17. return res * flag ;
    18. }
    19. int ne[N], h[N], e[N], w[N], idx;
    20. bool st[N];
    21. int dist[N];
    22. int n, m;
    23. queue<int> q;
    24. void add(int a, int b, int c) {
    25. e[idx] = b;
    26. ne[idx] = h[a];
    27. w[idx] = c;
    28. h[a] = idx ++;
    29. }
    30. int spfa() {
    31. memset(dist, 0x3f, sizeof dist);
    32. dist[1] = 0;
    33. q.push(1);
    34. st[1] = true;
    35. while (q.size()) {
    36. int u = q.front();
    37. q.pop();
    38. st[u] = false;
    39. for (int i = h[u]; i != -1; i = ne[i]) {
    40. int j = e[i];
    41. if (dist[j] > dist[u] + w[i]) {
    42. dist[j] = dist[u] + w[i];
    43. if(!st[j]) {
    44. st[j] = true;
    45. q.push(j);
    46. }
    47. }
    48. }
    49. }
    50. return dist[n];
    51. }
    52. int main() {
    53. memset(h, -1, sizeof h);
    54. n = read();
    55. m = read();
    56. while (m --) {
    57. int a, b, c;
    58. a = read();
    59. b = read();
    60. c = read();
    61. add(a, b, c);
    62. }
    63. int res = spfa();
    64. if (res == 0x3f3f3f3f) puts("impossible");
    65. else cout << res << endl;
    66. return 0;
    67. }

  • 相关阅读:
    全志V3S嵌入式驱动开发(开发环境再升级)
    【汇编语言02】第2章 寄存器——理论知识
    水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益
    Node.js基础知识、fs、path、http三大模块、nodejs的模块化、npm与包管理
    postgreSQL
    【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
    操作系统——进程与线程の选择题整理
    LeetCode 43. 字符串相乘
    vscode - 环境准备 - 修改缓存路径
    怎样利用数据讲一个精彩故事?
  • 原文地址:https://blog.csdn.net/beloved_yu/article/details/125551110
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号