Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5003 | Accepted: 1926 |
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
#include<cstdio>
#include<cstring>
const int INF=0x3f3f3f3f;
const int N=5002;
const int M=200010;
int head[N],dis[2][N],n,m,from[M],to[M],nxt[M],l[M],cnt;
void add(int x,int y,int f)
{
from[cnt]=x;
to[cnt]=y;
l[cnt]=f;
nxt[cnt]=head[x];
head[x]=cnt++;
}
void spfa(int s,int t)
{
int Q[N],front=0,rear=0,i,j,u;
bool inQ[N];
memset(dis[t],0x3f,sizeof(dis[t]));
memset(inQ,0,sizeof(inQ));
Q[(rear++)%N]=s;inQ[s]=1;dis[t][s]=0;
while(front%N!=rear%N)
{
i=Q[(front++)%N];inQ[i]=0;
for(j=head[i];j!=-1;j=nxt[j])
{
u=to[j];
if(dis[t][i]+l[j]<dis[t][u])
{
dis[t][u]=dis[t][i]+l[j];
if(!inQ[u]){Q[(rear++)%N]=u;inQ[u]=1;}
}
}
}
}
int main()
{
int a,b,c,i;
while(~scanf("%d%d",&n,&m))
{
cnt=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(b,a,c),add(a,b,c);
}
spfa(1,0);spfa(n,1);
int first=dis[0][n],min=INF;
if(first==INF){puts("-1");continue;}
for(i=0;i<cnt;i++)
{
int u=from[i],v=to[i];
int tmp=dis[0][u]+l[i]+dis[1][v];
if(tmp>first&&tmp<min)min=tmp;
}
printf("%d\n",min==INF?-1:min);
}
return 0;
}
思路二:一次spfa,用dis[i][k]保存从起点到i的前k短路(此题k=2),故此算法还可求k稍大的所谓第k短路
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
const int N=5002;
const int M=200010;
const int K=2;
int head[N],to[M],nxt[M],l[M],cnt,n;
void add(int x,int y,int f)
{
to[cnt]=y;
l[cnt]=f;
nxt[cnt]=head[x];
head[x]=cnt++;
}
int dis[N][K],siz[N];
bool find(int x,int u)
{
int l=0,r=siz[u]-1;
while(l<=r)
{
int mid=(l+r)>>1,tmp=dis[u][mid];
if(tmp==x)return 1;
else if(tmp>x)r=mid-1;
else l=mid+1;
}
return 0;
}
void spfa(int s,int t)
{
int i,j,it,u;
queue<int> Q;
bool inQ[N];
for(i=1;i<=n;i++)siz[i]=0,inQ[i]=0;
while(!Q.empty())Q.pop();
Q.push(s);inQ[s]=1;
dis[s][0]=0,siz[s]=1;
while(!Q.empty())
{
i=Q.front();Q.pop();inQ[i]=0;
for(j=head[i];j!=-1;j=nxt[j])
{
u=to[j];
for(it=0;it<siz[i];it++)
{
int t1=dis[i][it]+l[j];
if(find(t1,u))continue;
if(siz[u]<K){
dis[u][siz[u]]=t1;
siz[u]++;
sort(dis[u],dis[u]+siz[u]);
if(!inQ[u]){Q.push(u);inQ[u]=1;}
continue;
}
int t2=dis[u][K-1];
if(t1<t2)
{
dis[u][K-1]=t1;
sort(dis[u],dis[u]+siz[u]);
if(!inQ[u]){Q.push(u);inQ[u]=1;}
}
else break;
}
}
}
if(siz[t]<K)puts("-1");
else printf("%d\n",dis[t][K-1]);
}
int main()
{
int a,b,c,m;
while(~scanf("%d%d",&n,&m))
{
cnt=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
spfa(1,n);
}
return 0;
}
思路三:spfa+A*
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=5002;
const int M=200010;
int head[N],dis[N],n,m,to[M],nxt[M],l[M],cnt;
struct node
{
int to,len;
bool operator<(node p)const
{
return p.len+dis[p.to]<len+dis[to];
}
};
void add(int x,int y,int f)
{
to[cnt]=y;
l[cnt]=f;
nxt[cnt]=head[x];
head[x]=cnt++;
}
void spfa(int s)
{
int Q[N],front=0,rear=0,i,j,u;
bool inQ[N];
memset(dis,0x3f,sizeof(dis));
memset(inQ,0,sizeof(inQ));
Q[(rear++)%N]=s;inQ[s]=1;dis[s]=0;
while(front%N!=rear%N)
{
i=Q[(front++)%N];inQ[i]=0;
for(j=head[i];j!=-1;j=nxt[j])
{
u=to[j];
if(dis[i]+l[j]<dis[u])
{
dis[u]=dis[i]+l[j];
if(!inQ[u]){Q[(rear++)%N]=u;inQ[u]=1;}
}
}
}
}
int Astar(int k)
{
int vis[N]={0},i;
priority_queue<node> Q;
node p,q;
p.len=0,p.to=1;
Q.push(p);
while(!Q.empty())
{
p=Q.top(),Q.pop();
vis[p.to]++;
if(vis[p.to]==k)return p.len+dis[p.to];
for(i=head[p.to];i!=-1;i=nxt[i])
{
q.len=p.len+l[i];
q.to=to[i];
Q.push(q);
}
}
return INF;
}
int main()
{
int a,b,c;
while(~scanf("%d%d",&n,&m))
{
cnt=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(b,a,c),add(a,b,c);
}
spfa(n);
if(dis[1]==INF){puts("-1");continue;}
int min=Astar(2);
printf("%d\n",min>=INF?-1:min);
}
return 0;
}