POJ——3255题 Roadblocks(次最短路径 )

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/88982947

Roadblocks

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21422   Accepted: 7498

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)

本来这题没啥思路,学习网上大佬的方法后才跟着做了出来,谢谢大佬!!

需要确定的是必须使用邻接表的形式储存边,若以邻接矩阵则内存毕超。另外需要最短路径数组dis和标记数组book都需要开两个(dis[i][0]代表最短路径,dis[i][1]代表次短路径 。book数组也是一样)

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#include<stdio.h>
#define MAXN 5007
#define MAXR 100010
#define INF 0x3f3f3f3f 
using namespace std;
int n,r,top1=0;
int head[MAXN];
struct edge{    //邻接表
	int to,next,len;
}map1[100010*2];
void add(int a,int b,int c){  //向邻接表中添加数据
	map1[top1].to=b;
	map1[top1].len=c;
	map1[top1].next=head[a];
	head[a]=top1++;
}
int dij(){
	int dis[MAXR][2],book[MAXR][2];
	for(int i=1;i<=n;i++){
		dis[i][0]=dis[i][1]=INF;
		book[i][0]=book[i][1]=0;
	}
	dis[1][0]=0;//初始化将初始点到初始点置为零
	for(int i=1;i<=n*2;i++){
		int min1=INF,x,y;
		for(int j=1;j<=n;j++){   //找到一个没有被使用过且距离出发点最短的顶点
			if(!book[j][0]&&dis[j][0]<min1){
				x=j;y=0;min1=dis[x][y];
			}else if(!book[j][1]&&dis[j][1]<min1){
				x=j;y=1;min1=dis[x][y];
			}
		}
		if(min1==INF)break;  //若min1未改变,说明所有的顶点已经全部使用,退出循环
		book[x][y]=1;
		for(int j=head[x];j!=-1;j=map1[j].next){//从此点开始更新dis数组
			int s=map1[j].to;
			int dist=min1+map1[j].len;
			if(dis[s][0]>dist){  //若更新后,比最短路径还要短,则将最短路径以及次短路径都更新
				dis[s][1]=dis[s][0];
				dis[s][0]=dist;
			}else if(dis[s][1]>dist){//若只比次短路径短,则只更新次短路径
				dis[s][1]=dist;
			}
		}
	}
	return dis[n][1]; 
}
int main()
{
	int from,to,cost;
	top1=0;
	memset(head,-1,sizeof(head));
	cin>>n>>r;
	for(int i=0;i<r;i++){
		scanf("%d%d%d",&from,&to,&cost);
		add(from,to,cost);
		add(to,from,cost);//无向图,需要将路径反向添加一遍
	}
    int h=dij();
    cout<<h<<endl;
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/88982947
今日推荐