-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPFA.cpp
54 lines (54 loc) · 1.01 KB
/
SPFA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1007;
const int INF=0x7fffffff;
struct Edge {
int from ,to, dist;
Edge(int u,int v,int d):from(u),to(v),dist(d) {}
};
struct SPFA {
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
bool visit[maxn];
int d[maxn];
int p[maxn];
void init(int n) {
this->n=n;
for(int i=0; i<n; i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist) {
edges.push_back(Edge(from,to,dist));
m=edges.size();
G[from].push_back(m-1);
}
void spfa(int s) {
for(int i=1; i<=n; i++) {
d[i] = INF;
}
queue<int> q;
memset(visit,false,sizeof(visit));
q.push(s);
visit[s] = true;
d[s] = 0;
while(!q.empty()) {
int u = q.front();
visit[u] = false;
for(int i=0; i<G[u].size(); i++) {
Edge& e=edges[G[u][i]];
if (d[e.to]>d[u]+e.dist) {
d[e.to]=d[u]+e.dist;
//p[e.to] = u;
if( !visit[e.to] ) {
visit[e.to] = true;
q.push(e.to) ;
}
}
}
q.pop();
}
}
}sf;