vjudge提交链接
题目
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)
题意:
第一行给出n和m,n代表只有n的顶点,m代表m条双向边。
接下来m行分别代表着顶点u到顶点v的权值为w。
求:1到n的次短路,路可以重复走。
解题思路:
- 对于一条边,s-e,那么求一条从1到n的次短路,其实就是相当于求:从1到s的最短路+从e到n的最短路+s-e的边权值。
- 那么对于一个无向图,从1到s的最短路求一遍以1作为源点的单源最短路即可,那么从e到n的最短路呢?逆向思考一下,对于无向图,其实就是求一遍从n到e的最短路,那么解这个题,就是先求两边最短路,然后每条边枚举一下即可.
- 枚举每一条边,维护次小值即可。
所需算法:spfa+邻接表
为什么不用dijkstra+邻接表,还没考虑明白
怎样构建邻接表
构建邻接表函数void build(int x,int y,int z)
前提须知:题目给出m条边,这里我们假定有2m条边,因为是双向边嘛。编号1–2m,千万别纠结怎么编号,只要编好之后默认该编号即可。
前提须知:这里用数组来模拟指针,所需数组first[],nx[],to[],s[].
first数组:存储的就是以顶点i为始点的最后一条边所对应的编号,利用该编号可以逐步向前寻找仍以该点为始点的边所对应的编号。
nx数组:存储的是也是边的编号,只是j=first[i],j=nx[j],意思就是把相同始点边利用nx数组连接起来,因次上述的寻找,需要用到nx数组。
即 for(j=first[i];j!=-1;j=nx[j])
to数组:存储的就是每条边对应的终边,to[first[i]。
s数组:存储的就是每条边对应的权值,s[first[i]。
为何是下标是first[i],因为first[i]存放的是每条边的编号,这样可以对应的每条边不会乱套。
数组first的最小范围:n+1 (first[x])
数组nx的最小范围:2m+1
数组to的最小范围:2m+1
数组s的最小范围:2*m+1
spfa算法的理解
不太懂,略,参考啊哈算法吧。
AC代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=100100;
int book[N],dis1[N],dis2[N];
int first[2*N],nx[2*N],to[2*N],s[2*N];
int head,n,m;
void build(int x,int y,int z)
{
nx[head]=first[x];
first[x]=head;
s[head]=z;
to[head++]=y;
}
void spfa(int x,int dis[])
{
int i,u,v;
memset(book,0,sizeof(book));
dis[x]=0;
book[x]=1;
queue<int> q;
q.push(x);
while(!q.empty())
{
u=q.front();
q.pop();
book[u]=0;
for(i=first[u];i!=-1;i=nx[i])
{
v=to[i];
if(dis[v]>dis[u]+s[i])
{
dis[v]=dis[u]+s[i];
if(!book[v])
{
book[v]=1;
q.push(v);
}
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
int i,j;
memset(first,-1,sizeof(first));
memset(dis1,INF,sizeof(dis1));
memset(dis2,INF,sizeof(dis2));
head=1;
while(m--)
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
build(x,y,z);
build(y,x,z);
}
spfa(1,dis1);
spfa(n,dis2);
int ans,minn=INF;
for(i=1;i<=n;i++)
{
for(j=first[i];j!=-1;j=nx[j])
{
ans=dis1[i]+dis2[to[j]]+s[j];
if(ans>dis1[n]&&minn>ans)
minn=ans;
}
}
printf("%d\n",minn);
}
return 0;
}