Roadblocks
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: A, B, 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)
需要注意一下的就是双向路
先给Dijkstra的代码:
思路就是程序设计与挑战里面的。
时间:
//#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
using namespace std;
#define M 100005
int inf=0x3f3f3f3f;
vector<int>e[M],zhi[M];
typedef pair<int ,int> P;
int dis[M],n;
int dis2[M];//次短路
void Dijkstra(int s)
{
priority_queue<P, vector<P>,greater<P> >q;
fill(dis,dis+n+1,inf);
fill(dis2,dis2+n+1,inf);
dis[s]=0;
q.push(P(0,s));
while(!q.empty())
{
P p=q.top(); q.pop();
int u=p.second,/*顶点编号*/d=p.first/*距离*/;
if(dis2[u]<p.first) continue;
for(int i=0;i<e[u].size() ;i++){
int v=e[u][i];
int w=zhi[u][i];
int d2=d+w;
if(dis[v]>dis[u]+w)//更新最短路 ,这次的最短路就变为次短路 swap()
{
//dis[v]=dis[u]+w; 更新
swap(dis[v],d2);
q.push(P(dis[v],v));
}
if(dis2[v]>d2/*能更新*/&&dis[v]<d2/*且不是最短路*/)//该边不能更新最短路但是可以更新次短路
{
dis2[v]=d2;
q.push(P(dis2[v],v));
}
}
}
printf("%d\n",dis2[n]);
return;
}
int main(void)
{
int m,u,v,w;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d %d %d",&u,&v,&w);
e[u].push_back(v);
zhi[u].push_back(w);
e[v].push_back(u);
zhi[v].push_back(w);//双向
}
Dijkstra(1);
return 0;
}
SPFA求次短路比较复杂点,容易出错:
时间;
//SPFA开两个数组,一个存最短路一次存次短路,跑一遍就行
//#include<bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<cstring>
#define ll long long int
using namespace std;
ll inf=9999999999999999;
vector<int> e[5005];
vector<ll>zhi[5005];
int n,book[5005];
ll dis[2][5005];//定义个二维0表示最短路,1表示次短路
void SPFA(int x,int y)
{
int u,v,flag;
ll w;
memset(book,0,sizeof(book));
for(int i=1;i<=n;i++)
dis[0][i]=inf,dis[1][i]=inf;
dis[0][x]=0ll;//最短路
//dis[1][x]=0; //次短路不能
queue <int> q;//队列
q.push(x);
book[x]=1;
while(!q.empty())
{
u=q.front();q.pop();
for(int i=0;i<e[u].size();i++){//顶点u的所有出边
v=e[u][i];
w=zhi[u][i];
flag=0;
//最短路更新发生,新次短路也能更新
if(dis[0][v]>dis[0][u]+w)//x->v?=x->u->v;
{
//将 '上次的' '最短路' 更新为次短路
dis[1][v]=dis[0][v];//上次的最短路
dis[0][v]=dis[0][u]+w;//松弛成功,更新最短路
flag=1;
}
if(dis[1][v]>dis[0][u]+w&&dis[0][v]<dis[0][u]+w/*后面这一句是指u->v这条边不能更新最短路*/)//最短路不能更新,但这条u->v的最短路径次短路可以更新u->v的次短路径
{
dis[1][v]=dis[0][u]+w;
flag=1;
}
if(dis[1][v]>dis[1][u]+w)//这条边更新不了最短路,次短路
{
dis[1][v]=dis[1][u]+w;
flag=1;
}//SPFA:每次仅对最短路估计值发生了变化的顶点的所有出边进行松弛操作,次短路松弛过程也一样对于不能更新最短路的点也要进行松弛
if(flag==1)
{
if(book[v]==0)
{
book[v]=1;
q.push(v);
}
}
}
book[u]=0;//取消队首标记
}
//printf("%d\n",dis[0][y]);
//输出次短路
if(dis[1][y]==inf)
printf("no route\n");
else
printf("%lld\n",dis[1][y]);
return;
}
int main(void)
{
int m,x,y;
ll w;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d %d %lld",&x,&y,&w);
e[x].push_back(y);
zhi[x].push_back(w);
}
ll q;
scanf("%lld",&q);
while(q--)
{
int a,b;
scanf("%d %d",&a,&b);
SPFA(a,b);
}
return 0;
}