Java教程

最短路径算法

本文主要是介绍最短路径算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一:弗洛伊德

局限性   不能存在负环,不然答案就是负无穷

动态转移方程:g[i][j] = min(g[i][j], g[i][k] + g[k][j]);//g为中间点
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 101;
int g[N][N];
void Floyd(int n) {
	for (int k = 1; k <= n; k++) 
		for (int i = 1; i <= n; i++) 
			for (int j = 1; j <= n; j++) 
				g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
int main() {
	memset(g, 0x3f, sizeof(g));
	for (int i = 0; i < N; i++) g[i][i] = 0;
	int n, m;
	int u, v, w;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> u >> v >> w;
		g[u][v] = g[v][u] = w;
	}
	Floyd(n);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cout << g[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}

二:Dijkstra(堆优)

局限性   不能存在负环,不然答案就是负无穷

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
const int N = 100001;
const int inf = 0x3f3f3f3f;
struct node {
    int v, w;
    node() {
      
    }
    node(int vv, int ww) {
        v = vv;
        w = ww;
    }
};
vector<node> g[N];
int n, m, s;
int d[N];
set<pair<int, int> > min_heap;
int main() {
    cin >> n >> m >> s;
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back(node(v, w));
        g[v].push_back(node(u, w));
    }
    memset(d, 0x3f, sizeof(d));
    d[s] = 0;
    min_heap.insert(make_pair(0, s));
    while (min_heap.size()) {
        int mind = min_heap.begin() -> first;
        int v = min_heap.begin() ->second;
        min_heap.erase(min_heap.begin());
        for (int i = 0; i < g[v].size(); i++) {
            if (d[g[v][i].v] > d[v] + g[v][i].w) {
                min_heap.erase(make_pair(d[g[v][i].v], g[v][i].v));
                d[g[v][i].v] = d[v] + g[v][i].w;
                min_heap.insert(make_pair(d[g[v][i].v], g[v][i].v));
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << d[i] << " ";
    }
    return 0;
}

三:SPFA

shortest path

《faster》(?!)

algorithm

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100001;
const int inf = 0x3f3f3f3f;
struct node {
    int v, w;
    node() {
      
    }
    node(int vv, int ww) {
        v = vv;
        w = ww;
    }
};
vector<node> g[N];
int n, m, s;
int d[N];
bool in_queue[N];
queue<int> q;
int main() {
    cin >> n >> m >> s;
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back(node(v, w));
        g[v].push_back(node(u, w));
    }
    memset(d, 0x3f, sizeof(d));
    d[s] = 0;
    in_queue[s] = true;
    q.push(s);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        in_queue[v] = false;
        for (int i = 0; i < g[v].size(); i++) {
            int x = g[v][i].v;
            if (d[x] > d[v] + g[v][i].w) {
                d[x] = d[v] + g[v][i].w;
                if (!in_queue[x]) {
                    q.push(x);
                    in_queue[x] = true;
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << d[i] << " ";
    }
    return 0;
}

这篇关于最短路径算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!