笔记③:POJ Roadblocks 次短路问题代码解析(优先队列逆序排列两种方法)

Roadblocks(寻找次短路)
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3466 Accepted: 1309

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)

题目描述:

给你n个点,m条边。问你从1号点到n号点的次短路是多少。(需要注意,一条边可以重复走多次)

自己敲的代码及解析如下:

#include<iostream>
#include<queue>
#include<vector>
#include<string>
#include<functional>//这个包含后面优先队列重载递减运算子greater<P>模板
using namespace std;
int R, N;
const int MAXN = 10001;
const int INF = 100001;
int dist[MAXN], dist2[MAXN];
//dist储存最短边,dist2储存次短边
struct edge {
	int to;
	int cost;
	edge() {};
	edge(int a, int b) :to(a), cost(b) {  };
};
vector<edge>G[MAXN];
struct P {
int d;
int point;
P() {};
P(int a, int b) :point(a), d(b) {};
bool operator<(const P &a) const{
	return d > a.d;
}
};
priority_queue<P>que;
//也可以不用重载<运算符和自定义P结构体,实现如下:
//P的结构体用pair及其默认参数p.first, p.second
//typedef pair<int, int>P;
//priority_queue<P, vector<P>, greater<P> >que;
//在后面会给出代码
void solve() {
	while (!que.empty()) {
		que.pop();
	}
	for (int i = 0; i <= N; i++) {
		dist[i] = dist2[i] = INF;
	}//初始化为无穷大
	dist[1] = 0;//起始点1到1的距离为0
	que.push(P(1, 0));//起始点1和它到自己的距离0
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int u = p.point;
		if (dist2[u] < p.d)continue;//发现一条边大于次短边,没有更新的必要
		for (int i = 0; i < G[u].size(); i++)
		{
			edge &e = G[u][i];
			if (e.cost + p.d < dist[e.to]) {
				//发现更短的边,最短边和次短边同时更新,再把最短边放入队列
				//此时原来的最短边变成次短边已经被更新过了,就不用再放入了
				dist2[e.to] = dist[e.to];
				dist[e.to] = p.d + e.cost;
				que.push(P(e.to, dist[e.to]));
			}
			if (dist2[e.to] > e.cost + p.d&&e.cost + p.d>dist[e.to]) {
				//有一条边长度在最短边和次短边之间,那么这条边就要更新为新的次短边
				//也就是更新为dist2[e.to],然后再放进队列
				dist2[e.to] = e.cost + p.d;
				que.push(P(e.to, dist2[e.to]));
				//记住,次短边也要放入队列中,因为每次是更新最短边和次短边,需要有两个比较
			}
		}
	}
	cout << dist2[N] << endl;
}
int main() {
	scanf("%d %d", &N, &R);
	int a, b, c;
	for (int i = 0; i < R; i++) {
		scanf("%d%d%d", &a, &b, &c);
		G[a].push_back(edge(b, c));
		G[b].push_back(edge(a, c));
		//由于道路是双向的,所以两边都要
	}
	solve();
	system("pause");
}
输出结果如下:




上述代码涉及到Dijkstra优先队列的运用,现在做一点补充:

优先队列的默认排序是从大到小,上述代码实现了从小到大的排序,方法是在自定义结构体P中重载运算符号 <

bool operator<(const P &a) const{
	return d > a.d;
}
};
priority_queue<P>que;
通过将 < 重新定义为 > 的作用,改变了排序方式,即原先的< 符号在优先队列内部的排序中意义改变了,使得原先的从大到小排序变成我们所需要的从小到大的排序。(也就是<此时我们强制将它变成大于的意思)

除了这种重载运算符的方法,我们其实直接用priority_queue<P, vector<P>, greater<P>>que;就不用重载<运算符了,

而且不需要自定义结构体,用库自带的pair,即typedef pair<int, int>P;(上述运算子greater还需要包含库 #include<functional>)

代码如下(可以与上面的进行对比,会发现P里面的参数 point 和 d 被默认的 first 和second代替了,正因greater运算子排序与pair参数first 和 second 兼容(也就是可以先对first实参排序再对second实参排序)所以可以不用重载比较符):

#include<iostream>
#include<queue>
#include<vector>
#include<string>
#include<functional>//这个包含后面优先队列重载递减运算子greater<P>模板
using namespace std;
int R, N;
const int MAXN = 10001;
const int INF = 100001;
int dist[MAXN], dist2[MAXN];
//dist储存最短边,dist2储存次短边
struct edge {
	int to;
	int cost;
	edge() {};
	edge(int a, int b) :to(a), cost(b) {  };
};
vector<edge>G[MAXN];
typedef pair<int, int>P;
priority_queue<P, vector<P>, greater<P> >que;//只需要两行就可以实现上一个代码中自定义结构体和运算符重载
void solve() {
	while (!que.empty()) {
		que.pop();
	}
	for (int i = 0; i <= N; i++) {
		dist[i] = dist2[i] = INF;
	}
	dist[1] = 0;
	que.push(P(1, 0));
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int u = p.first;//p(pair)的参数只有first和second,此处的first相当于point,second相当于d
		if (dist2[u] < p.second)continue;
		for (int i = 0; i < G[u].size(); i++)
		{
			edge &e = G[u][i];
			if (e.cost + p.second < dist[e.to]) {
				dist2[e.to] = dist[e.to];
				dist[e.to] = p.second + e.cost;
				que.push(P(e.to, dist[e.to]));
			}
			if (dist2[e.to] > e.cost + p.second&&e.cost + p.second>dist[e.to]) {
				dist2[e.to] = e.cost + p.second;
				que.push(P(e.to, dist2[e.to]));
			}
		}
	}
	cout << dist2[N] << endl;
}
int main() {
	scanf("%d %d", &N, &R);
	int a, b, c;
	for (int i = 0; i < R; i++) {
		scanf("%d%d%d", &a, &b, &c);
		G[a].push_back(edge(b, c));
		G[b].push_back(edge(a, c));
	}
	solve();
	system("pause");
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值