传送门
题目描述
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).
贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友。贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路。 贝茜所在的乡村有R(1<=R<=100,000)条双向道路,每条路都联结了所有的N(1<=N<=5000)个农场中的某两个。贝茜居住在农场1,她的朋友们居住在农场N(即贝茜每次旅行的目的地)。 贝茜选择的第二短的路径中,可以包含任何一条在最短路中出现的道路,并且,一条路可以重复走多次。当然咯,第二短路的长度必须严格大于最短路(可能有多条)的长度,但它的长度必须不大于所有除最短路外的路径的长度。
输入输出格式
输入格式:
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)
输出格式:
Line 1: The length of the second shortest path between node 1 and node N
输入输出样例
输入样例#1:
4 4
1 2 100
2 4 200
2 3 250
3 4 100
输出样例#1:
450
简单一点,就是要在一个有向图中找到节点1到节点n的次短路。
方法一
在初学OI时,有道题时求一个序列中给定区间内的最大值和次大值,当然数据很小,直接跑一遍打擂台就OK了,只用注意新的数能否更新当前最大值或次大值。
这题也是类似,我们依然用堆优化的dijkstra算法,dis数组由一维更新为二维,dis[i][0]为最短路长度,dis[i][1]为次短路长度,方法不变。
现在谈一谈用dijkstra(下简称DJ)做次短路的一些细节问题。
#include
using namespace std;
inline int read(){
char ch;
int flag=1;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') flag=-1;
int ans=ch-48;
while((ch=getchar())>='0'&&ch<='9') ans=ans*10+ch-48;
return ans*flag;
}
inline void write(int x){
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar('0'+x%10);
return;
}
int n,m,cnt=0;
const int N=1e5+1;
struct node{
int v,w,nt;
}e[2*N];
int head[5001],dis[5001][2];
inline void add(int u,int v,int w){
cnt++;
e[cnt]=(node){v,w,head[u]};
head[u]=cnt;return;
}
inline void diji(){
memset(dis,0x3f,sizeof(dis));
dis[1][0]=0;
typedef pair<int,int>T;
priority_queue<T,vector<T>,greater<T> >q;
q.push(make_pair(0,1));
while(q.size()){
pair<int ,int> t=q.top();
q.pop();
int d=t.first,u=t.second;
for(int i=head[u];i;i=e[i].nt){
int v=e[i].v;
if(dis[v][0]>d+e[i].w){
dis[v][1]=dis[v][0];
dis[v][0]=d+e[i].w;
q.push(make_pair(dis[v][0],v));
}
else if(d+e[i].w<dis[v][1]){
dis[v][1]=d+e[i].w;
q.push(make_pair(dis[v][1],v));
}
}
}
}
int main(){
n=read();m=read();
int u,v,w;
for(int i=1;i<=m;i++){
u=read(),v=read(),w=read();
add(u,v,w);add(v,u,w);
}
diji();
write(dis[n][1]);
return 0;
}
算法2:两次spfa(博主太菜了于是贴了神仙lucario的代码%%%%%%%%)
#include
using namespace std;
inline int read(){
char ch;
int x=0,f=1;
ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
return x*f;
}
struct edge{
int u,v,w,nxt;
}e[200010];
int first[5010],cnt,n,m;
inline void add(int u,int v,int w){
e[++cnt]=(edge){u,v,w,first[u]};first[u]=cnt;
}
int dis[5010],cop[5010];
bool vis[5010];
void spfa(int st){
deque<int>q;
memset(dis,0x3f,sizeof(dis));
memset (vis,0,sizeof(vis));
dis[st]=0;
q.push_back(st);
vis[st]=1;
while(!q.empty()){
int u=q.front();q.pop_front();
vis[u]=0;
for(int i=first[u];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;
if(!vis[v]){
if(q.empty()||dis[v]<dis[q.front()])
q.push_front(v);
else
q.push_back(v);
vis[v]=1;
}
}
}
}
}
int main(){
n=read();m=read();
int u,v,w;
for(int i=1;i<=m;i++){
u=read();v=read();w=read();
add(u,v,w);add(v,u,w);
}
spfa(1);
copy(dis+1,dis+n+1,cop+1);
spfa(n);
int minn=dis[1],secmin=0x3f3f3f3f;
for(int i=1;i<=cnt;i++){
u=e[i].u;v=e[i].v;w=e[i].w;
int ans=dis[v]+w+cop[u];
if(ans>minn)secmin=min(ans,secmin);
}
cout<<secmin;
return 0;
}