Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11063 | Accepted: 3921 |
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
Source
#include<iostream>
#include<functional>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
#define QWQ ios::sync_with_stdio(0)
#define inf 0x3f3f3f3f
typedef unsigned long long LL;
typedef long long ll;
const int T = 500000+50;
const int mod = 1000000007;
int n,m,c;
int head[T],tail[T];
struct node
{
int v,next,w;
}e1[T],e2[T];
struct line
{
int f,v,g;
bool operator<(const line& b)const{
return f>b.f||(f==b.f&&g>b.g);
}
};
void Init()
{
c = 0;
fill(head,head+T,-1);
fill(tail,tail+T,-1);
}
void add(int x,int y,int w)
{
e1[c].v = y,e1[c].next = head[x];
e1[c].w = w;
head[x] = c;
e2[c].v = x,e2[c].next = tail[y];
e2[c].w = w;
tail[y] = c++;
e1[c].v = x,e1[c].next = head[y];
e1[c].w = w;
head[y] = c;
e2[c].v = y,e2[c].next = tail[x];
e2[c].w = w;
tail[x] = c++;
}
int dis[T];
bool vis[T];
void spfa()//要从终点扩展
{
fill(dis,dis+T,inf);
fill(vis,vis+T,false);
dis[n] = 0;
queue<int> Q;
Q.push(n);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = tail[u];~i;i=e2[i].next){
int v = e2[i].v;
int w = e2[i].w;
if(dis[v]>dis[u]+w){
dis[v] = dis[u] + w;
if(!vis[v]){
Q.push(v);
vis[v] = true;
}
}
}
}
}
int Astar()
{
priority_queue<line> A;
line t,tt;
t.v = 1,t.g = 0,t.f = t.g + dis[1];
if(dis[1]==inf)return -1;
A.push(t);
int cnt = 0;
while(!A.empty())
{
tt = A.top();A.pop();
if(tt.v==n){
cnt++;
if(cnt==2){
return tt.g;
}
}
for(int i=head[tt.v];~i;i=e1[i].next){
t.v = e1[i].v;
t.g = tt.g + e1[i].w;//起点值
t.f = t.g + dis[t.v];//终点值
A.push(t);
}
}
return -1;
}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int i,j;
int u,v,w;
while(~scanf("%d%d",&n,&m))
{
Init();
for(i=1;i<=m;++i){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
spfa();
printf("%d\n",Astar());
}
return 0;
}