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
题目链接:http://poj.org/problem?id=3255
代码如下:
<span style="font-size:24px;">#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
#include<cstdio>
#include<climits>
using namespace std;
const int maxx=100001;
const int INF=INT_MAX/10;
int n,r;
int d[maxx],ju=INF,di[maxx];
struct My
{
int to,cost;
My(int a,int b)
{
to=a;
cost=b;
}
};
vector<My>G[maxx];
void init()
{
for(int i=0; i<n; i++)G[i].clear();
}
void add_edge(int from,int to,int cost)
{
G[from].push_back(My(to,cost));
}
typedef pair<int,int>p;
void dijikstra(int s)
{
priority_queue<p,vector<p>,greater<p> >que;
que.push(p(0,s));
for(int i=0; i<=n; i++)d[i]=INF;
d[s]=0;
for(int i=0; i<=n; i++)di[i]=INF;
while(!que.empty())
{
p p1=que.top();
que.pop();
int v=p1.second;
if(di[v]<p1.first) continue;
for(int i=0; i<G[v].size(); i++)
{
My &e=G[v][i];
int num=p1.first+e.cost;
if(d[e.to]>num)
{
swap(d[e.to],num);
que.push(p(d[e.to],e.to));
}
if(di[e.to]>num&&d[e.to]<num)
{
di[e.to]=num;
que.push(p(di[e.to],e.to));
}
}
}
}
int main()
{
int a,b,c;
scanf("%d%d",&n,&r);
for(int i=0; i<r; i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
dijikstra(1);
cout<<di[n]<<endl;
return 0;
}</span>
这是标准解法,就是将每次的最短路和次短路都存到优先队列里,然后一个一个取出计算。
还有一种我自己想的,感觉思路没错,但是不知道哪个地方出了问题。。反正没A。
我的想法是这样的,因为对于dij算法而言,每一步都是取当前的最小值。起点为1,终点为N。则1--B的最短路是由1--A(B前面的一个点)的最短路加上AB。我们需要知道的是,由于这是选择次短路,那么只有可能是某一个相邻两点的最短路变长了一点。不可能出现两个地方都变长的情况,这样就不是次短路了。然后就可以枚举相邻两点的最小边长距离,然后取最小值即可。这就是最短路与次短路的距离差。
代码如下:没能AC:
<span style="font-size:24px;">#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
#include<cstdio>
#include<climits>
using namespace std;
const int maxx=100001;
const int INF=INT_MAX/10;
int n,r;
int d[maxx],ju=INF,di[maxx],pre[maxx]= {0};
struct My
{
int to,cost;
My(int a,int b)
{
to=a;
cost=b;
}
};
vector<My>G[maxx];
void init()
{
for(int i=0; i<n; i++)G[i].clear();
}
void add_edge(int from,int to,int cost)
{
G[from].push_back(My(to,cost));
}
typedef pair<int,int>p;
void dijikstra(int s)
{
priority_queue<p,vector<p>,greater<p> >que;
que.push(p(0,s));
for(int i=0; i<=n; i++)d[i]=INF;
d[s]=0;
for(int i=0; i<=n; i++)di[i]=INF;
di[s]=0;
while(!que.empty())
{
p p1=que.top();
que.pop();
int v=p1.second;
if(d[v]<p1.first)
{
continue;
}
for(int i=0; i<G[v].size(); i++)
{
My e=G[v][i];
if(d[e.to]>d[v]+e.cost)
{
di[e.to]=min(di[e.to],d[e.to]);
pre[e.to]=v;
d[e.to]=d[v]+e.cost;
que.push(p(d[e.to],e.to));
}
else
{
di[e.to]=min(di[e.to],d[v]+e.cost);
}
}
}
}
int main()
{
int a,b,c;
scanf("%d%d",&n,&r);
for(int i=0; i<r; i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
dijikstra(1);
for(int t=n; t!=1; t=pre[t])
{
ju=min(ju,di[t]-d[t]);
}
cout<<d[n]+ju<<endl;
return 0;
}</span>
<span style="font-size:24px;">欢迎大神指出错误。。。</span>