poj 3255 Roadblocks

本文介绍了解决次短路径问题的三种方法:通过两次SPFA算法分别计算起点和终点到各节点的最短路径并结合每条边进行枚举;使用一次SPFA算法计算前k短路径;以及结合SPFA与A*算法来寻找次短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Roadblocks
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

Line 1: Two space-separated integers: N and R 
Lines 2..R+1: Each line contains three space-separated integers: AB, 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)
 
分析:次短路问题(次短路是严格比最大路大的)
思路一:两次spfa,分别求出以s和t为起点到其他点的最短路,保存到dis[0]和dis[1]中,然后枚举每条边,假设该边的起点和终点为u,v,边权为l[i],此时求出有一条长为dis[0][u]+l[i]+dis[1][v]的路径,在这些路径中求出次大的即次短路。
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值