F - Roadblocks(次短路)

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)
网易翻译:
贝西已经搬到一个小农场去了,她有时喜欢回去看望她的一个好朋友。她不想很快就到达老家,因为她喜欢沿途的风景。她决定走第二短的而不是最短的路。她知道一定有第二最短的路径。乡村由R (1 R 100,000)双向公路组成,每条公路连接两个N (1 N 5000)十字路口,方便地编号为1…Bessie从十字路口1开始,而她的朋友(目的地)则在十字路口n。第二最短路径可以与任何一条最短路径共享道路,也可以重复使用同一条道路或十字路口。第二最短路径是指长度大于最短路径的最短路径(即如果存在两条或两条以上的最短路径,则第二最短路径是长度大于这两条路径但不大于其他路径的路径)。输入行1:两个用空格分隔的整数:N和R。R + 1:每一行包含三个空格分隔的整数:A、B和D描述道路连接路口A和B, D长度(1 D 5000)输出1行:第二节点之间的最短路径的长度和节点N示例输入4 4 1 2 100 2 4 2 3 200 250 3 450 100示例输出提示两个路线:1→2→4(长度100+200=300)and 1 ->2→3→4(长度100 + 250 + 100 = 450)

#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<map>
using namespace std;
typedef long long ll;
#define N 6666
const int INF = 0x3f3f3f;
typedef pair<int,int>P;
typedef struct node{
	int to,cost;//to表示当前点,cost表示路径长(权重) 
}lala;
vector<lala>container[N];
int dist[N];//最短距离
int distc[N];//次短距离 
int n,r;
int dijkstra()
{
	memset(dist,INF,sizeof(dist));
	memset(distc,INF,sizeof(distc));
	priority_queue <P,vector<P>,greater<P> >q;//队列优化,从小到大(出来的数是从大到小的,在队列中除了第一个数是最小的,其他数是乱序的 
	dist[1]=0; 
	q.push(P(0,1));//把距离为0,地点位置为1的数据放到队列中
	while(!q.empty())
	{
		P p=q.top();//找到队列最前面的点
		q.pop();//该点弹出
		int dis=p.first;//pair中的第一个数据,表示从起点到当前点的距离
		int v=p.second;//pair中的第二个数据,表示当前点,也是结构体中的to 
		if(dis>distc[v])//此时该点路径大于到当前点的次短路径,就舍弃它
		continue;
		for(int i=0;i<container[v].size();i++)
		{
			lala la=container[v][i];//起点为v的i条边
			int dd=dis+la.cost;
			if(dd<dist[la.to]) //更新最短路 
			{
				swap(dist[la.to],dd);//为了储存次短路
				q.push(P(dist[la.to],la.to));//每次都要把更新后的放到队列中
				 
			}
			if(dd>dist[la.to]&&dd<distc[la.to]) //更新次短路
			{
				distc[la.to]=dd;//将次短路更新为dd 
				q.push(P(distc[la.to],la.to));
			 } 
		 } 
	 } 
	 return distc[n];
	
}
int main() 
{
    
	cin>>n>>r;
	while(r--)
	{
		int a,b,w;
		cin>>a>>b>>w;
		container[a].push_back(lala{b,w});
		container[b].push_back(lala{a,w});
		
	 } 
	 cout<<dijkstra()<<endl;
    return 0;
}

//详细在代码注释中,但c++过不了(显示编译错误),用g++才过,但本人并不知道为啥。。。。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值