Johnson算法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Johnson算法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2704字,纯文字阅读大概需要4分钟。
内容图文
![Johnson算法](/upload/InfoBanner/zyjiaocheng/677/83449ae7fe6747cca49958467ce3a358.jpg)
求多源负权最短路时用,比\(floyd\)快,\(O(VE + V^2logV)\)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a,b,sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e_Line printf("\n---------------\n")
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "TIME : %.3lfms\n", (double)clock() * 1.0 / (double)CLOCKS_PER_SEC)
#endif
struct FastIO {
template<typename ATP> inline FastIO& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
if(f == -1) x = -x;
return *this;
}
} io;
using namespace std;
template<typename ATP> inline ATP Max(ATP x, ATP y) {
return x > y ? x : y;
}
template<typename ATP> inline ATP Min(ATP x, ATP y) {
return x < y ? x : y;
}
const int N = 5007;
struct Edge {
int nxt, pre, w, from;
} e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v, int w) {
e[++cntEdge] = (Edge){ head[u], v, w, u}, head[u] = cntEdge;
}
int DIS[N][N], H[N], vis[N];
int qq[N], h, t;
int n, m;
inline void SPFA(int st) {
for(register int i = 1; i <= n; i += 3){
H[i] = 0x3f3f3f3f;
H[i + 1] = 0x3f3f3f3f;
H[i + 2] = 0x3f3f3f3f;
}
H[st] = 0;
qq[++t] = st;
while(h != t){
int u = qq[++h];
if(h >= N - 5) h = 0;
vis[u] = false;
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(H[v] > H[u] + e[i].w){
H[v] = H[u] + e[i].w;
if(!vis[v]){
vis[v] = true;
qq[++t] = v;
if(t >= N - 5) t = 0;
}
}
}
}
}
struct nod {
int x, w;
bool operator < (const nod &com) const {
return w > com.w;
}
};
#include <queue>
priority_queue<nod> q;
int dis[N];
inline void Dijkstra(int st) {
for(register int i = 1; i <= n; i += 3){
dis[i] = 0x3f3f3f3f;
dis[i + 1] = 0x3f3f3f3f;
dis[i + 2] = 0x3f3f3f3f;
}
dis[st] = 0;
q.push((nod){ st, 0});
while(!q.empty()){
int u = q.top().x, w = q.top().w;
q.pop();
if(w != dis[u]) continue;
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(dis[v] > dis[u] + e[i].w){
dis[v] = dis[u] + e[i].w;
q.push((nod){ v, dis[v]});
}
}
}
}
int main() {
io >> n >> m;
R(i,1,m){
int u, v, w;
io >> u >> v >> w;
add(u, v, w);
}
R(i,1,n){
add(0, i, 0);
}
SPFA(0);
R(i,1,cntEdge){
e[i].w += H[e[i].from] - H[e[i].pre];
}
R(i,1,n){
Dijkstra(i);
R(j,1,n){
DIS[i][j] = dis[j] - H[i] + H[j];
}
}
R(i,1,n){
R(j,1,n){
printf("%d ", DIS[i][j]);
}
putchar('\n');
}
return 0;
}
/*
5 5
1 2 9
1 4 7
2 4 -3
2 3 11
5 2 -2
*/
内容总结
以上是互联网集市为您收集整理的Johnson算法全部内容,希望文章能够帮你解决Johnson算法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。