1267. 路障(block.pas/c/cpp) 
(File IO): input:block.in output:block.out

Time Limits: 1000 ms  Memory Limits: 65536 KB  Detailed Limits  

Goto ProblemSet

Description

  Bessie 来到一个小农场,有时她想回老家看看她的一位好友。她不想太早地回到老家,因为她喜欢途中的美丽风景。她决定选择次短路径,而不是最短路径。
  农村有 R (1 <= R <= 100,000) 条双向的路,每条路连接 N (1 <= N <= 5000) 个结点中的两个。结点的编号是 1..N。Bessie 从结点 1出发,她的朋友(目的地)在结点 N。
  次短路径可以使用最短路径上的路,而且允许退回,即到达一个结点超过一次。次短路径是一种长度大于最短路径的路径(如果存在两条或多条最短路径存在,次短路径就是比它们长,且不比其他任何的路径长的路径)。
 

Input

  Line 1: 两个用空格分隔的整数 N 和 R
  Lines 2..R+1: 每行包含三个用空格分隔的整数: A, B, 和 D表示有一条路连接结点A和B,长度为D (1 <= D <= 5000)。

Output

  Line 1: 结点 1 到结点 N的次短路径长度。
 

Sample Input

  1. 4 4
    1 2 100
    2 4 200
    2 3 250
    3 4 100

Sample Output

  1. 450
 
做法:其实次短路可以用spfa在更新最短路的同时更新一下次短路就好了,但我头铁打了A*
 
代码如下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <string>
  4. #include <iostream>
  5. #include <queue>
  6. #include <algorithm>
  7. #include <cstdlib>
  8. #define N 400007
  9. using namespace std;
  10. int n, m, tot, ans;
  11. struct edge
  12. {
  13. int to, next, w;
  14. }e[N];
  15. int ls[N], f[N], list[N];
  16. int times[N];
  17. bool v[N];
  18. struct A_node
  19. {
  20. int g, h, p;
  21. bool operator < (A_node x) const
  22. {
  23. return x.g + x.h < g + h;
  24. }
  25. };
  26. priority_queue<A_node>Q;
  27.  
  28. void add(int x, int y, int z)
  29. {
  30. e[++tot].to = y;
  31. e[tot].next = ls[x];
  32. e[tot].w = z;
  33. ls[x] = tot;
  34. e[++tot].to = x;
  35. e[tot].next = ls[y];
  36. e[tot].w = z;
  37. ls[y] = tot;
  38. }
  39.  
  40. void spfa()
  41. {
  42. for (int i = ; i <= n; i++)
  43. f[i] = ;
  44. f[n] = ;
  45. int h = , t = ;
  46. list[++t] = n;
  47. v[n] = ;
  48. while (h < t)
  49. {
  50. int p = list[++h];
  51. for (int i = ls[p]; i; i = e[i].next)
  52. if (f[p] + e[i].w < f[e[i].to])
  53. {
  54. f[e[i].to] = f[p] + e[i].w;
  55. if (!v[e[i].to])
  56. {
  57. v[e[i].to] = ;
  58. list[++t] = e[i].to;
  59. }
  60. }
  61. v[p] = ;
  62. }
  63. }
  64.  
  65. int A_star()
  66. {
  67. A_node t1, tmp;
  68. t1.p = , t1.g = , t1.h = ;
  69. Q.push(t1);
  70. while (!Q.empty())
  71. {
  72. t1 = Q.top(); Q.pop();
  73. times[t1.p]++;
  74. if (times[t1.p] == && t1.p == n) return t1.h + t1.g;
  75. if (times[t1.p] > ) continue;
  76. for (int i = ls[t1.p]; i; i = e[i].next)
  77. {
  78. tmp.p = e[i].to;
  79. tmp.g = f[e[i].to];
  80. tmp.h = e[i].w + t1.h;
  81. Q.push(tmp);
  82. }
  83. }
  84.  
  85. }
  86.  
  87. int main()
  88. {
  89. freopen("block.in", "r", stdin);
  90. freopen("block.out", "w", stdout);
  91. scanf("%d%d", &n, &m);
  92. int x, y, z;
  93. for (int i = ; i<= m; i++)
  94. {
  95. scanf("%d%d%d", &x, &y, &z);
  96. add(x, y, z);
  97. }
  98. spfa();
  99. ans = A_star();
  100. printf("%d", ans);
  101. }

JZOJ 1267. 路障的更多相关文章

  1. 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法

    彻底解决phpcms v9升级后,文章发布出现: MySQL Query : SELECT * FROM `withli_a`.`v9_keyword` WHERE `keyword` = '吼吼' ...

  2. 洛谷 P3395 路障

    P3395 路障 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1,1)这个点,他要走到(n,n)这个点. B君每秒可以向上下左右的某个方 ...

  3. (jzoj snow的追寻)线段树维护树的直径

    jzoj snow的追寻 DFS序上搞 合并暴力和,记录最长链和当前最远点,距离跑LCA # include <stdio.h> # include <stdlib.h> # ...

  4. 并发库应用之八 & 循环路障CyclicBarrier应用

    JDK包位置:java.util.concurrent.CyclicBarrier 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及 ...

  5. django插入数据库错误:mysql的1267错误

    错误信息: django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IM ...

  6. [jzoj]3506.【NOIP2013模拟11.4A组】善良的精灵(fairy)(深度优先生成树)

    Link https://jzoj.net/senior/#main/show/3506 Description 从前有一个善良的精灵. 一天,一个年轻人B找到她并请他预言他的未来.这个精灵透过他的水 ...

  7. [jzoj]3468.【NOIP2013模拟联考7】OSU!(osu)

    Link https://jzoj.net/senior/#main/show/3468 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: ...

  8. [jzoj]5478.【NOIP2017提高组正式赛】列队

    Link https://jzoj.net/senior/#main/show/5478 Description Sylvia 是一个热爱学习的女孩子.       前段时间,Sylvia 参加了学校 ...

  9. [jzoj]1115.【HNOI2008】GT考试

    Link https://jzoj.net/senior/#main/show/1115 Description 申准备报名参加GT考试,准考证号为n位数X1X2X3...Xn-1Xn(0<=X ...

随机推荐

  1. Hive项目实战:用Hive分析“余额宝”躺着赚大钱背后的逻辑

    一.项目背景 前两年,支付宝推出的“余额宝”赚尽无数人的眼球,同时也吸引的大量的小额资金进入.“余额宝”把用户的散钱利息提高到了年化收益率4.0%左右,比起银行活期存储存款0.3%左右高出太多了,也正 ...

  2. javascript模块化编程规范

    一.javascript模块化编程规范: 二.关于commenjs规范和AMD规范: 根本不同:前者用于服务器端同步加载模块:后者是客户端异步加载模块. 同点:两者都有一个全局函数require(), ...

  3. java字符串与二进制的相互转化

    public class StrBinaryTurn { // 将Unicode字符串转换成bool型数组 public boolean[] StrToBool(String input) { boo ...

  4. 设置VS代码模板

    本文URL:http://www.cnblogs.com/CUIT-DX037/p/6770366.html 打开VS安装目录下:\Microsoft Visual Studio 12.0\Commo ...

  5. 使用SpringSession管理分布式会话时遇到的反序列化问题

    关于SpringSession相关的介绍和使用指南,可移步如下网址: [SpringSession管理分布式系统的会话Session] https://www.cnblogs.com/captaina ...

  6. 【干货】JavaScript DOM编程艺术学习笔记4-6

    四.案例研究:JavaScript图片库 js: function showPic(whichpic){ //取得链接 var source=whichpic.getAttribute("h ...

  7. h5 本地存储

    H5本地存储有两个API,一个是Web Storage,还有一个是Web SQL.不管是哪一个,都是基于JavaScript语言来使用,接下来我就教你怎么使用H5本地存储,本文篇幅较大,JS代码较多, ...

  8. jeesite应用实战(数据增删改查),认真读完后10分钟就能开发一个模块

    jeesite配置指南(官方文档有坑,我把坑填了!)这篇文章里,我主要把jeesite官方给出的帮助文档的坑填了,按照里面的方法可以搭建起来jeesite的站点.系统可以运行以后,就可以进入开发模块了 ...

  9. Openfire+spark在linux上搭建内部聊天系统

    一.    实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...

  10. java cpu使用率高异常排查

    1.top命令对cpu进行排序shift+p 2.pwdx pid查找业务进程路径 3.top -Hp pid查看相关负载线程pid 4.printf “0x%x\n” 线程pid     // 将线 ...