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
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
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
Sample Output
450
Hint
/
// // //
// Created by Team 3 //
// Copyright (c) 2015年 Team 3. All rights reserved. //
/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
#define INF 1<<31
#define cir(i,a,b) for (int i=a;i<=b;i++)
#define CIR(j,a,b) for (int j=a;j>=b;j--)
#define CLR(x) memset(x,0,sizeof(x))
typedef long long ll;
using namespace std;
#define maxn 200010
int pnt[maxn],pre[maxn],head[maxn],w[maxn];
long long cnt;
long long R;
int N;
bool vis[maxn];
long long d[maxn],cd[maxn];
void addedge(int u,int v,int weight)
{
pnt[cnt]=v;w[cnt]=weight;
pre[cnt]=head[u];head[u]=cnt++;
}
void spfa(int s,long long *dt)
{
queue<int> q;
memset(vis,false,sizeof(vis));
// memset(dt,0x3f,sizeof(dt));
dt[s]=0;
vis[s]=true;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=false;
for (long long i=head[x];i!=-1;i=pre[i])
{
int v=pnt[i];
if (dt[v]>dt[x]+w[i])
{
dt[v]=dt[x]+w[i];
if (!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
}
int main()
{
while (cin >> N >> R)
{
memset(d,0x3f,sizeof(d));
memset(cd,0x3f,sizeof(cd));
cnt=0;
memset(head,-1,sizeof(head));
memset(pre,-1,sizeof(pre));
for (long long i=1;i<=R;i++)
{
int u,v,dis;
cin >> u >> v >>dis;
addedge(u,v,dis);
addedge(v,u,dis);
}
spfa(1,d);
spfa(N,cd);
// cout << d[N] << endl;
// cout << cd[N]<< endl;
long long ans=0x3f3f,temp;
for (int i=1;i<=N;i++)
{
for (long long j=head[i];j!=-1;j=pre[j])
{
int v=pnt[j];
temp=d[i]+cd[v]+w[j];
if (temp>d[N] && ans>temp)
ans=temp;
}
}
cout << ans << endl;
}
return 0;
}