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

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)

Source


d[u][0]和d[u][1]分别最短路和次短路
更新时类似DP求树的直径
spfa:有一个点的最短路或次短路更新了,把这个店加进去
dijkstra:hn结构体中多一个p,0最短路,1次短路
 
//spfa 32MS NO.1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][],inq[N],q[N],head=,tail=;
void spfa(){
for(int i=;i<=n;i++){d[i][]=d[i][]=INF;}
d[][]=; inq[]=; q[++tail]=;
while(head<=tail){
int u=q[head++];//printf("u %d\n",u);
inq[u]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v][]>d[u][]+w){
d[v][]=d[v][];
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}else if(d[v][]>d[u][]+w&&d[v][]<d[u][]+w){
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}
if(d[v][]>d[u][]+w){
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
spfa();
printf("%d",d[n][]);
return ;
}
//dijkstra 63MS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][],vis[N][];
struct hn{
int u,d,p;
hn(int a=,int b=,int c=):u(a),d(b),p(c){}
bool operator < (const hn &rhs)const{return d>rhs.d;}
};
priority_queue<hn> q;
void dijkstra(){
for(int i=;i<=n;i++) {d[i][]=d[i][]=INF;}
q.push(hn(,,));
d[][]=;
while(!q.empty()){
hn now=q.top();q.pop();
int u=now.u,p=now.p;
if(vis[u][p]) continue;
vis[u][p]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v][]>d[u][p]+w){
d[v][]=d[v][];
d[v][]=d[u][p]+w;
q.push(hn(v,d[v][],));
q.push(hn(v,d[v][],));
}else if(d[v][]>d[u][p]+w){
d[v][]=d[u][p]+w;
q.push(hn(v,d[v][],));
}
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
dijkstra();
printf("%d",d[n][]);
return ;
}

最新文章

  1. Win7下安装Centos7
  2. My SQL的内连接,外链接查询
  3. NFS挂载操作指南
  4. [crunch bang]在Crunch Bang安装和设置fcitx(小企鹅输入法)
  5. python并行迭代
  6. UICollectionViewLayout
  7. 银联接口(注意项&amp;备忘)
  8. MYSQL auto_increment 、default 关键字
  9. 在ASP.NET应用中执行后台任务
  10. Git add和commit步骤分析
  11. ViewPager+Fragment实现滑动切换页面
  12. Centos7 编译安装 Nginx PHP Mariadb Memcache扩展 ZendOpcache扩展 (实测 笔记 Centos 7.0 + Mariadb 10.1.9 + Nginx 1.9.9 + PHP 5.5.30)
  13. MySQL5.7 JSON类型及其相关函数的学习
  14. 纯js星级评分
  15. Promise是什么?
  16. Vue.js结合vue-router和webpack编写单页路由项目
  17. pagehelper调用mybatis报错java.lang.NoSuchMethodError:org.apache.ibatis.reflection.MetaObject.forObject
  18. SpringSecurity自定义登陆页面和跳转页面
  19. 在Tomcat中部署Spring jpetstore
  20. maven配置(安装&amp;使用&amp;私服)文档

热门文章

  1. 原生JS:Array对象详解
  2. JavaScript强化教程——jQuery AJAX 实例
  3. spring-hellow word
  4. php强制转换类型和CMS远程管理插件的危险
  5. Android上的MVP:如何组织显示层的内容
  6. Android自定义View滑动事件处理总结
  7. 【C语言】C语言外部变量和内部变量
  8. Android 系统API实现数据库的增删改查和SQLite3工具的使用
  9. 【原】你真的懂iOS的autorelease吗?
  10. 常用jar包用途