The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1…N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input
Line 1: Two space-separated integers: N and R
Lines 2… R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Line 1: The length of the second shortest path between node 1 and node N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

第一次接触次短路的问题,原来接触过次小生成树的问题,现在也忘了。
求解次短路也是按着最短路的套路来。
从1到n的次短路等于dis1[i]+p[i][j]+dis2[j],其中dis1代表着顶点一到顶点i的最短路,dis2代表着n到j的最短路,再加上之间的距离就好了。其中用的vector,spfa求解最短路
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;const int maxx=5e3+10;
struct node{int v;int w;
}pp;
vector<node>p[maxx];
int dis1[maxx];
int dis2[maxx];
int vis[maxx];
int n,m;void spfa(int u,int *dis)
{queue<int >q;memset(vis,0,sizeof(vis));vis[u]=1;dis[u]=0;q.push(u);while(!q.empty()){u=q.front();q.pop();for(int i=0;i<p[u].size();i++){int v=p[u][i].v;int w=p[u][i].w;if(dis[v]>dis[u]+w){dis[v]=dis[u]+w;if(!vis[v]){vis[v]=1;q.push(v);}}}}
}int main()
{scanf("%d%d",&n,&m);memset(dis1,inf,sizeof(dis1));memset(dis2,inf,sizeof(dis2));for(int i=0;i<m;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);pp.v=b;pp.w=c;p[a].push_back(pp);pp.v=a;pp.w=c;p[b].push_back(pp);}spfa(1,dis1);spfa(n,dis2);int ans=inf;for(int i=1;i<=n;i++){for(int j=0;j<p[i].size();j++){int v=p[i][j].v;int w=p[i][j].w;int ant=dis1[i]+dis2[v]+w;if(ant<ans&&ant>dis1[n]){ans=ant;}}}printf("%d\n",ans);
}

努力加油a啊,(o)/~

Roadblocks(次短路经)相关推荐

  1. [BZOJ1726][Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1277  Solved: 607 ...

  2. bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路(A*第k短路)

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1324  Solved: 627 ...

  3. POJ3255 Roadblocks ——次短路

    Roadblocks 题意:求次短路 思路:见注释,用dijkstra同时维护最短路和次短路 // Decline is inevitable // Romance will last forever ...

  4. 【BZOJ】1726 [Usaco2006 Nov]Roadblocks第二短路

    [算法]最短路(spfa) 次短路 [题解] 正反跑两次SPFA,然后枚举每一条边,如果起点到一个端点的最短路+另一个端点到终点的最短路+长度 ≠ 最短路,则和答案比较,保存最小值. #include ...

  5. [Usaco2006 Nov]Roadblocks第二短路

    贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路. 贝茜所在的 ...

  6. POJ 3255 Roadblocks 次短路

    和Dijksta求最短路一样,只是要维护两个数组:最短路d1,次短路d2.然后更新的时候注意细节. //#pragma comment(linker, "/STACK:1024000000, ...

  7. POJ - 3255 Roadblocks(次短路)

    题目链接:点击查看 题目大意:求次短路 题目分析:唉,限时训练的时候遇到的这个题,明明之前系统学习过次短路和次小生成树这个方面的知识的,可能是很长时间没 用,板子都忘掉了,不过还好,在考试的时候想到了 ...

  8. 【bzoj1726/Usaco2006 Nov】Roadblocks第二短路——SPFA

    题目链接 分析:题目要求一个连通图的从1到n的严格次短路,我们只需要在跑最短路的时候顺便判一下次短路是否能够被更新即可. dis[x][0]表示1到x的最短路,而dis[x][1]则表示次短路,需要分 ...

  9. 题目推荐—BZOJ 水题推荐

    [bzoj1756]Vijos1083小白逛公园 裸题- -..线段树维护lmax,rmax,max,sum然后搞之.. [Ahoi2008]Meet 紧急集合 求两两点的lca然后会发现必然有两个l ...

  10. POJ 图论分类 + DP(较全 自己又加了点)

    DP -----------动态规划 状态压缩DP 2411 (棋盘规模较大)状态压缩DP+DFS+滚动数组 2664 (棋盘规模较小)直接递推即可(DP) 2506 (棋盘规模较小)直接递推即可(D ...

最新文章

  1. JSON Web Tokens测试工具
  2. Castle DynamicProxy基本用法(AOP)
  3. PyTorch 《动手学深度学习》学习笔记(Dive-into-DL-Pytorch)
  4. Centos7-firewall-cmd
  5. SVG.use/拖拽
  6. [MyBatisPlus]乐观锁和悲观锁
  7. Redis问的太深入,面试官说:“你先回去等通知吧“!
  8. ios 监听数组个数的变化_iOS 监听数组的变化
  9. 修改oracle安装目录权限设置权限,oracle rac环境,安装目录权限被修改乱了,怎么恢复?...
  10. 浅谈Spark几种不同的任务提交相关脚本(以Spark 1.5.0为例)
  11. redis 实战系列(一)
  12. JZOJ1286太空电梯
  13. linux系统的ftp命令大全,Linux-FTP命令大全(完整版)
  14. 读书笔记-采购与供应链管理(一个实践者的角度)
  15. windows无法完成安装 若要在此计算机上安装_Win10无法启动,主引导记录(MBR)损坏,用这个方法快速修复...
  16. 数列极限的概念及性质
  17. java 用户留存率_【java】mongodb 数据统计(留存率) 应该怎么实现?
  18. 全球与中国量身定制生产线市场深度研究分析报告
  19. 计算机网口速率修改,win7系统修改无线网卡连接速率的操作方法
  20. 学习Matlab的第一个程序——用二分法求根

热门文章

  1. php signature解密,openssl RSA非对称加密、解密、签名、验签
  2. SVN使用和解决方案
  3. mathematica练习程序(图像取反)
  4. 【转载】TCP和TCP/IP的区别
  5. MyGeneration学习笔记(5) :在Web Service中使用dOOdad(中)
  6. Incompatible JavaHL library loaded. Subversion 1.8.x required.
  7. json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)...
  8. MySql查找几个字段的值一样的记录
  9. FreeBSD下安装postfixl邮件系统
  10. 打造个性化的Blog