SPFA求次短路


#include <bits/stdc++.h>
#include <queue>
using namespace std;
int n,m,vis[5010],dis[5010][2],head[5010],nxt[200010],to[200010],tot,eg[200010];

int read() {
	int sum=0,fg=1;
	char c=getchar();
	while(c<'0'||c>'9') {
		if(c=='-')fg=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') {
		sum=sum*10+c-'0';
		c=getchar();
	}
	return sum*fg;
}

void add(int x,int y,int c){
	tot++;
	nxt[tot]=head[x];
	to[tot]=y;
	eg[tot]=c;
	head[x]=tot;
}

void spfa(int s) {
	memset(dis,0x3f3f3f,sizeof(dis));
	dis[s][0]=0;
	//dis[i][0] means getting to point i the fastest way
	//dis[i][1] means getting to point i the second fastest way
	vis[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty()) {
		int now=q.front();
		q.pop();
		vis[now]=0;
		for(int i=head[now]; i; i=nxt[i]) {
			int v=to[i];
			if(dis[v][0]>dis[now][0]+eg[i]) {
				//only the fastest way can make another fastest way
				//the second fastest way can not do so
				//if there is a way faster than the fastest way than the fastest way will change to be the now fastest
				//and the second fastest way will become the old fastest way
				dis[v][1]=dis[v][0];
				dis[v][0]=dis[now][0]+eg[i];
				if(!vis[v]) {
					q.push(v);
					vis[v]=1;
				}

			}
			if(dis[v][1]>dis[now][0]+eg[i]&&dis[v][0]<dis[now][0]+eg[i]) {
				//if there exists a way faster than the second fastest way but slower than the fastest way
				//then change second fastest way
				
				//if there is a way that the fastest way can remake the second fastest way but can not remake the fastest way
				//then only change the second fastest way
				dis[v][1]=dis[now][0]+eg[i];
				if(!vis[v]) {
					q.push(v);
					vis[v]=1;
				}
			}
			if(dis[v][1]>dis[now][1]+eg[i]) {
				
				//anyway the second fasteset way can't anyhow become the fastest way so if there is 
				//a way that the new second fastest way can be faster than the now second fastest way then replace
				
				//this is only possible to make the second fastest way
				dis[v][1]=dis[now][1]+eg[i];
				if(!vis[v]) {
					q.push(v);
					vis[v]=1;
				}
			}
		}
	}
}

int main() {
	n=read();
	m=read();
	for(int i=1; i<=m; i++) {
		int a,b,c;
		a=read();
		b=read();
		c=read();
		add(a,b,c);
		add(b,a,c);
	}
	spfa(1);
	cout<<dis[n][1];

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SPFA算法(Shortest Path Faster Algorithm)是一种用于解决最短路问题的算法。它是一种单源最短路径算法,可以解决带有负权边的图的最短路径问题。 SPFA算法基本思想是使用队列对图中的所有节点进行遍历,对于每一个节点,如果它的邻居节点的最短路径可以通过当前节点更新,则将邻居节点加入队列中进行下一轮遍历,直到所有节点的最短路径都被更新后停止遍历。 在Java中,可以使用邻接矩阵或邻接表来表示图,并使用队列来实现SPFA算法。下面是一个使用邻接矩阵实现SPFA算法的Java代码示例: ```java import java.util.*; public class SPFA { public static void main(String[] args) { int[][] graph = { {0, 2, 5, Integer.MAX_VALUE, Integer.MAX_VALUE}, {Integer.MAX_VALUE, 0, 7, 1, Integer.MAX_VALUE}, {Integer.MAX_VALUE, Integer.MAX_VALUE, 0, 4, Integer.MAX_VALUE}, {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 0, 3}, {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 0} }; int[] dist = shortestPath(graph, 0); System.out.println(Arrays.toString(dist)); } public static int[] shortestPath(int[][] graph, int start) { int n = graph.length; int[] dist = new int[n]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; Queue<Integer> queue = new LinkedList<>(); queue.offer(start); boolean[] inQueue = new boolean[n]; inQueue[start] = true; while (!queue.isEmpty()) { int u = queue.poll(); inQueue[u] = false; for (int v = 0; v < n; v++) { if (graph[u][v] != Integer.MAX_VALUE && dist[v] > dist[u] + graph[u][v]) { dist[v] = dist[u] + graph[u][v]; if (!inQueue[v]) { queue.offer(v); inQueue[v] = true; } } } } return dist; } } ``` 在上面的代码中,我们使用一个二维数组`graph`来表示图,其中`graph[i][j]`表示从节点`i`到节点`j`的边的权重,如果没有边则为`Integer.MAX_VALUE`。函数`shortestPath`接受一个图和一个起点`start`,返回一个数组`dist`,其中`dist[i]`表示从起点`start`到节点`i`的最短路径。 在函数中,我们首先初始化`dist`数组为`Integer.MAX_VALUE`,表示所有节点到起点的距离都是无限大。然后将起点`start`加入队列中,并标记为已加入队列。进入循环后,每次取出队列中的一个节点`u`,将`u`标记为未加入队列,然后遍历`u`的所有邻居节点`v`,如果从起点到`v`的距离可以通过从起点到`u`再加上`u`到`v`的距离来更新,则更新`dist[v]`的值,并将`v`加入队列中,并标记为已加入队列。当队列为空时,所有节点的最短路径都已被更新,函数返回`dist`数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值