Roadblocks (spfa+ adjacency table, construction of two-way edge adjacency table)

vjudge submit link

topic

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: A, B, 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)

Title:

The first line gives n and m, where n represents only n vertices, and m represents m bidirectional edges.
The next m rows respectively represent the weights from vertex u to vertex v as w.
Ask: 1 to n times short circuit, the road can be repeated.

Problem-solving ideas:

  1. For an edge, se, then finding a secondary short circuit from 1 to n is actually equivalent to finding: the shortest shortest from 1 to s + the shortest shortest from e to n + the edge weight of se.
  2. So for an undirected graph, the shortest path from 1 to s is just to find the single-source shortest path with 1 as the source. What about the shortest path from e to n? Think in reverse. For an undirected graph, it is actually to find the shortest path from n to e. Then, to solve this problem, first find the shortest path on both sides, and then enumerate each side.
  3. Enumerate each edge and maintain the second smallest value.

Algorithm required: spfa+ adjacency table
Why not use dijkstra+ adjacency table, I haven’t considered it yet

How to construct adjacency list

Build adjacency list function void build(int x, int y, int z)

Prerequisites : The title is given with m edges. Here we assume that there are 2m edges because it is a two-way edge. Number 1–2m, don't worry about how to number, just default the number after editing.

Prerequisites : here we use arrays to simulate pointers. The required arrays are first[], nx[], to[], s[].

first array : Stored is the number corresponding to the last edge with vertex i as the starting point. With this number, you can step forward to find the number corresponding to the edge that still starts with this point.
nx array : It stores the number of the edge, but j=first[i], j=nx[j], which means to connect the edges with the same starting point using the nx array. Therefore, the above search requires the nx array.

即 for(j=first[i];j!=-1;j=nx[j])

to array : Stored is the end edge corresponding to each edge, to[first[i].
s array : Stored is the weight corresponding to each edge, s[first[i].

Why is the subscript first[i], because first[i] stores the number of each edge, so that each edge corresponding to it will not be messed up.

Minimum range of array first: n+1 (first[x])
Minimum range of array nx: 2 m+1
Minimum range of array to: 2
m+1
Minimum range of array s: 2*m+1

Understanding of spfa algorithm

I don't understand, just refer to the aha algorithm.

AC code

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=100100;
int book[N],dis1[N],dis2[N];
int first[2*N],nx[2*N],to[2*N],s[2*N];
int head,n,m;
void build(int x,int y,int z)
{
	nx[head]=first[x];
	first[x]=head;
	s[head]=z; 
	to[head++]=y;
}
void spfa(int x,int dis[])
{
	int i,u,v;
	memset(book,0,sizeof(book));
	dis[x]=0;
	book[x]=1;
	queue<int> q;
	q.push(x);
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		book[u]=0;
		for(i=first[u];i!=-1;i=nx[i])
		{
			v=to[i];
			if(dis[v]>dis[u]+s[i])
			{
				dis[v]=dis[u]+s[i];
				if(!book[v])
				{
					book[v]=1;
					q.push(v); 
				}
			}
		}
	}
}
int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		int i,j;
		memset(first,-1,sizeof(first));
		memset(dis1,INF,sizeof(dis1));
		memset(dis2,INF,sizeof(dis2));
		head=1;
		while(m--)
		{
			int x,y,z;
			scanf("%d %d %d",&x,&y,&z);
			build(x,y,z);
			build(y,x,z);
		}
		spfa(1,dis1);
		spfa(n,dis2);
		int ans,minn=INF;
		for(i=1;i<=n;i++)
		{
			for(j=first[i];j!=-1;j=nx[j])
			{
				ans=dis1[i]+dis2[to[j]]+s[j];
				if(ans>dis1[n]&&minn>ans)
					minn=ans;
			}
		}
		printf("%d\n",minn);	
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/Helinshan/article/details/114597122