Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12697   Accepted: 4491

Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

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: AB, 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

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

Sample Output

  1. 450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

Source


d[u][0]和d[u][1]分别最短路和次短路
更新时类似DP求树的直径
spfa:有一个点的最短路或次短路更新了,把这个店加进去
dijkstra:hn结构体中多一个p,0最短路,1次短路
 
  1. //spfa 32MS NO.1
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cstring>
  7. using namespace std;
  8. const int N=,M=,INF=1e9;
  9. inline int read(){
  10. char c=getchar();int x=,f=;
  11. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  12. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  13. return x;
  14. }
  15. int n,m,u,v,w;
  16. struct edge{
  17. int v,w,ne;
  18. }e[M<<];
  19. int h[N],cnt=;
  20. inline void ins(int u,int v,int w){
  21. cnt++;
  22. e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
  23. cnt++;
  24. e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
  25. }
  26. int d[N][],inq[N],q[N],head=,tail=;
  27. void spfa(){
  28. for(int i=;i<=n;i++){d[i][]=d[i][]=INF;}
  29. d[][]=; inq[]=; q[++tail]=;
  30. while(head<=tail){
  31. int u=q[head++];//printf("u %d\n",u);
  32. inq[u]=;
  33. for(int i=h[u];i;i=e[i].ne){
  34. int v=e[i].v,w=e[i].w;
  35. if(d[v][]>d[u][]+w){
  36. d[v][]=d[v][];
  37. d[v][]=d[u][]+w;
  38. if(!inq[v]){inq[v]=;q[++tail]=v;}
  39. }else if(d[v][]>d[u][]+w&&d[v][]<d[u][]+w){
  40. d[v][]=d[u][]+w;
  41. if(!inq[v]){inq[v]=;q[++tail]=v;}
  42. }
  43. if(d[v][]>d[u][]+w){
  44. d[v][]=d[u][]+w;
  45. if(!inq[v]){inq[v]=;q[++tail]=v;}
  46. }
  47. }
  48. }
  49. }
  50. int main(int argc, const char * argv[]) {
  51. n=read();m=read();
  52. for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
  53. spfa();
  54. printf("%d",d[n][]);
  55. return ;
  56. }
  1. //dijkstra 63MS
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cstring>
  7. using namespace std;
  8. const int N=,M=,INF=1e9;
  9. inline int read(){
  10. char c=getchar();int x=,f=;
  11. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  12. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  13. return x;
  14. }
  15. int n,m,u,v,w;
  16. struct edge{
  17. int v,w,ne;
  18. }e[M<<];
  19. int h[N],cnt=;
  20. inline void ins(int u,int v,int w){
  21. cnt++;
  22. e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
  23. cnt++;
  24. e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
  25. }
  26. int d[N][],vis[N][];
  27. struct hn{
  28. int u,d,p;
  29. hn(int a=,int b=,int c=):u(a),d(b),p(c){}
  30. bool operator < (const hn &rhs)const{return d>rhs.d;}
  31. };
  32. priority_queue<hn> q;
  33. void dijkstra(){
  34. for(int i=;i<=n;i++) {d[i][]=d[i][]=INF;}
  35. q.push(hn(,,));
  36. d[][]=;
  37. while(!q.empty()){
  38. hn now=q.top();q.pop();
  39. int u=now.u,p=now.p;
  40. if(vis[u][p]) continue;
  41. vis[u][p]=;
  42. for(int i=h[u];i;i=e[i].ne){
  43. int v=e[i].v,w=e[i].w;
  44. if(d[v][]>d[u][p]+w){
  45. d[v][]=d[v][];
  46. d[v][]=d[u][p]+w;
  47. q.push(hn(v,d[v][],));
  48. q.push(hn(v,d[v][],));
  49. }else if(d[v][]>d[u][p]+w){
  50. d[v][]=d[u][p]+w;
  51. q.push(hn(v,d[v][],));
  52. }
  53. }
  54. }
  55. }
  56. int main(int argc, const char * argv[]) {
  57. n=read();m=read();
  58. for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
  59. dijkstra();
  60. printf("%d",d[n][]);
  61. return ;
  62. }

POJ3255Roadblocks[次短路]的更多相关文章

  1. POJ3255-Roadblocks(最短路)

    Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...

  2. POJ-3255-Roadblocks(次短路的另一种求法)

    Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. Sh ...

  3. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  4. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  5. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  6. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  7. bzoj1266最短路+最小割

    本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...

  8. HDU2433 BFS最短路

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. 最短路(代码来源于kuangbin和百度)

    最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...

随机推荐

  1. HTML <select> 标签 创建单选或多选菜单

    所有主流浏览器都支持 <select> 标签. select 元素可创建单选或多选菜单. <select&> 元素中的 <option> 标签用于定义列表中 ...

  2. [SharePoint] SharePoint 错误集 2

    1 Run command “New-SPConfigurationDatabase" Feature Description: error message popup after run ...

  3. The quieter you become,The more you are able to hear.

  4. APP远程调试及网络自动化测试

    一:优测 腾讯旗下的测试服务 http://utest.qq.com/ 二:云测 http://www.testin.cn/ 三:testbird 1.进入这个网站,注册并且登录 https://dt ...

  5. Double 数据保留两位小数二:直接截取小数后面两位,不进行四舍五入

    package com; public class T2 { public static void main(String[] args) { System.out.println(calculate ...

  6. jax-rs中的一些参数标注简介(@PathParam,@QueryParam,@MatrixParam,@HeaderParam,@FormParam,@CookieParam)

    先复习一下url的组成: scheme:[//[user:password@]host[:port]][/]path[?query][#fragment] jax-rs anotation @Path ...

  7. 【代码笔记】iOS-轮询弹出框

    一,效果图. 二,工程图. 三,代码. RootViewController.m #import "RootViewController.h" //加入弹出框的头文件 #impor ...

  8. XCLNetTools1.0(ASP.NET常用类库)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 2016-01-01开放所有源代码: 项目地址:https://github.com/xucongli1989/XCLNetTools 下载地址: ...

  9. 微信企业号开发之-如何获取secret 序列号

    最近有项目基于微信企业号开发,简单记录下如何查看企业号secert 工具/原料 微信企业号   方法/步骤  用管理员的帐号登录后,选择[设置]-[权限管理]进入管理组设置界面      在左边点击[ ...

  10. centos7 添加图形界面的功能

    一般安装centos最小版的时候默认没有图形界面 需要自己安装 sudo  yum groupinstall "GNOME Desktop" "Graphical Adm ...