Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12433 | Accepted: 4398 |
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
题解:
这一题是求两点之间的次短路,若要求a到b的次短路,先以a为起点进行一次spfa,得到的数组为d,再以b为起点进行spfa,得到数组为d1,然后枚举所给的每一条直连边,假设端点为u,v,边长为w,求得每一个d[u]+d1[v]+w,取这些中大于最短路的最小的一个就是次短路。
/*
次短路板子
次短路求解方法:若要求a到b的次短路,先以a为起点进行一次spfa,得到的数组为d,再以b为
起点进行spfa,得到数组为d1,然后枚举所给的每一条直连边,假设端点为u,v,边长为w,
求得每一个d[u]+d1[v]+w,取这些中大于最短路的最小的一个就是次短路.
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=5000+10;
const int maxm=200000+10;
const int inf=0x3f3f3f3f;
int d[maxn],d1[maxn],head[maxn],inq[maxn],q[maxn];
int tol=0;
int n,r;
struct Edge
{
int from,to,w,next;
}edge[maxm];
void init()
{
memset(head,-1,sizeof(head));
tol=0;
}
void addedge(int u,int v,int w)
{
edge[tol].from=u,edge[tol].to=v,edge[tol].w=w,edge[tol].next=head[u],head[u]=tol++;
edge[tol].from=v,edge[tol].to=u,edge[tol].w=w,edge[tol].next=head[v],head[v]=tol++;
}
void spfa(int s,int *d)
{
memset(inq,0,sizeof(inq));
memset(q,0,sizeof(q));
for(int i=1;i<=n;i++) d[i]=inf;
d[s]=0,inq[s]=1;
int front=0,rear=0;
q[rear++]=s;
while(front!=rear)
{
int u=q[front++];
inq[u]=0;
if(front>=maxn) front=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(inq[v]) continue;
inq[v]=1;
q[rear++]=v;
if(rear>=maxn) rear=0;
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&r))
{
init();
while(r--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
spfa(1,d);
spfa(n,d1);
int ans=d[n],res=inf;
for(int i=0;i<tol;i++)
{
int u=edge[i].from,v=edge[i].to,w=edge[i].w;
if(d[u]+d1[v]+w>ans)
res=min(res,d[u]+d1[v]+w);
}
printf("%d\n",res);
}
}
这一题还有一种解法,用dijkstra算最短路的同时更新源点到该点的次短路,通过这题更深刻的理解力dijkstra算法。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=5000+10;
const int maxm=200000+10;
const int inf=0x3f3f3f3f;
int d[maxn],d1[maxn],head[maxn];
int tol=0;
int n,r;
typedef pair<int,int> P;
struct Edge
{
int to,w,next;
}edge[maxm];
void init()
{
memset(head,-1,sizeof(head));
tol=0;
}
void addedge(int u,int v,int w)
{
edge[tol].to=v,edge[tol].w=w,edge[tol].next=head[u],head[u]=tol++;
edge[tol].to=u,edge[tol].w=w,edge[tol].next=head[v],head[v]=tol++;
}
void dijkstra()
{
priority_queue<pair<int,int> >Q;
for(int i=1;i<=n;i++) d[i]=inf;
for(int i=1;i<=n;i++) d1[i]=inf;
d[1]=0;
Q.push(make_pair(-d[1],1));
while(!Q.empty())
{
P cur=Q.top();
Q.pop();
int u=cur.second,w=-cur.first;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
int c=edge[i].w+w;
if(c<d[v])
{
d[v]=c;
Q.push(make_pair(-d[v],v));
}
if(c<d1[v]&&c>d[v])
{
d1[v]=c;
Q.push(make_pair(-d1[v],v));
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&r))
{
init();
while(r--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
dijkstra();
printf("%d\n",d1[n]);
}
}