poj 3255 Roadblocks(次短路板子题)

Roadblocks
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

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)

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]);
	}
}





















  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值