多对端点之间的最短路

Description

N cities named with numbers 1 … N are connected with one-way roads. For each pair of cities i and j, you need to find the shortest path from city i to city j.

Input

The first line contains three integers N, K and Q (2<=N<=100, 1<=K<=10000, 1<=Q<=10000). N is the number of cities, K is the number of roads, and Q is the number of queries. Each of following K lines contains three integers i, j, d (1<=i,j<=N, 0<d<10000), indicates there is a road from city i to city j, and its length is d. The next Q lines describes the queries, each line contains two integers i and j (1<=i,j<=N).

Output

For each query, you need to print the shortest path in one line. If there is no path between the query cities pair, you should print “-1”.

Sample Input

4 3 2
1 2 3
2 3 4
2 4 3
1 3
2 4

Sample Output

7
3

分析

这道题主要是求多对端点之间的最短路,所以用Floyd-Warshall算法。

代码

#include <iostream>
using namespace std;

const int MAXN = 101;
//const int MAXK = 10001;
//const int MAXQ = 10001;
int dist[MAXN][MAXN];

void init() {
	for (int i = 0; i < MAXN; i++) {
		for (int j = 0; j < MAXN; j++) {
			dist[i][j] = -1;
		}
	}
}

int main() {
	//初始化
	init();
	int N, K, Q;
	cin >> N >> K >> Q;
	for (int i = 0; i < N; i++) {
		dist[i][i] = 0;
	}
	for (int i = 0; i < K; i++) {
		int b, d, s;
		cin >> b >> d >> s;
		if (dist[b - 1][d - 1] == -1 or dist[b - 1][d - 1] > s)
			dist[b - 1][d - 1] = s;
	}
	//Floyd-Warshall算法
	for (int k = 0; k < N; k++) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if ((dist[i][k] != -1 and dist[k][j] != -1) and(dist[i][j] == -1 or dist[i][j] > dist[i][k] + dist[k][j])) {
					dist[i][j] = dist[i][k] + dist[k][j];
				}
			}
		}
	}
	//输出
	for (int i = 0; i < Q; i++) {
		int b, d;
		cin >> b >> d;
		cout << dist[b - 1][d - 1] << endl;
	}
}

完成!

### SPFA算法概述 SPFA(Shortest Path Faster Algorithm)是一种基于Bellman-Ford算法的改进版本,主要用于求解含有负权边的单源短路径问题。它通过引入队列来加速松弛操作的过程,在实际应用中表现出了较高的效率[^2]。 SPFA的核心思想是对图中的每条边进行松弛操作,直到无法进一步更新为止。与传统的Bellman-Ford相比,SPFA仅对那些可能会影响其他节点距离值的顶点执行松弛操作,从而减少了不必要的计算开销[^3]。 --- ### SPFA算法实现 以下是SPFA算法的一个基本实现: #### 邻接表建图 SPFA通常采用邻接表的方式存储图结构,这有助于减少空间消耗并提高访问速度。 ```cpp #include <iostream> #include <queue> #include <vector> #include <cstring> using namespace std; const int INF = 0x3f3f3f3f; struct Edge { int to, weight; }; int n, m, s; // 节点数、边数、起点编号 vector<Edge> adj[1000]; // 邻接表 bool inQueue[1000]; long long dist[1000]; void spfa(int start) { queue<int> q; memset(dist, 0x3f, sizeof(dist)); // 初始化为无穷大 memset(inQueue, false, sizeof(inQueue)); dist[start] = 0; q.push(start); inQueue[start] = true; while (!q.empty()) { int u = q.front(); q.pop(); inQueue[u] = false; for (auto &edge : adj[u]) { // 对u的所有邻居进行遍历 if (dist[edge.to] > dist[u] + edge.weight) { // 松弛条件 dist[edge.to] = dist[u] + edge.weight; if (!inQueue[edge.to]) { // 如果未入队则加入队列 q.push(edge.to); inQueue[edge.to] = true; } } } } } ``` 上述代码实现了SPFA的基本逻辑,其中`adj[]`是一个邻接表数组,用于记录每个节点的相邻关系及其权重;`dist[]`保存从起始节点到各节点的当前短距离;`inQueue[]`标记某个节点是否已经在队列中以防止重复入队[^4]。 --- ### 算法优化策略 尽管SPFA在许场景下表现出色,但在极端情况下其时间复杂度退化至O(VE),因此需要采取一些措施加以优化: 1. **SLF(Small Label First)** 在每次将新节点压入队列之前,优先考虑将其插入队首而非队尾。具体来说,如果待插入节点的距离小于等于队头元素的距离,则应插于队首;反之则正常追加到队尾。这种方法能够显著提升某些特定测试用例下的性能。 2. **LLL(Large Label Last)** 当发现某次迭代后的小距离大于某一阈值时,可以跳过后续部分运算过程,因为这些较大的数值很可能不会影响终结果。不过需要注意的是,这种剪枝方式可能会破坏正确性,需谨慎使用。 3. **端点检测机制** 若存在个候选终点,则可在程序结束前提前终止搜索流程一旦确认任意目标可达即可返回相应答案而无需继续完成整个遍历工作流[^1]。 --- ### 复杂度分析 理论上讲,SPFA的时间复杂度介于O(E)和O(VE)之间,取决于输入数据的具体特性以及所选优化手段的效果如何。对于稀疏图而言,由于平均下来每个结点只会经历有限次数进出队列的动作,因而整体耗时往往接近线性级别;然而当面对稠密网络或者特殊构造的数据集时,就有可能触发差情形下的平方级增长趋势。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值