最短路径
Dijkstra算法:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <vector> 5 #include <climits> 6 using namespace std; 7 8 int N, M, S, T; 9 vector<vector<int> > m(1001, vector<int>(1001)); 10 vector<int> dist(1001); 11 vector<int> s(1001); 12 13 void solve() { 14 dist.assign(N+1, INT_MAX); 15 s.assign(N+1, false); 16 for (int i = 1; i <= N; ++i) { 17 dist[i] = m[S][i]; 18 } 19 dist[S] = 0; s[S] = true; 20 for (int i = 1; i <= N; ++i) { 21 int tmp_min = INT_MAX; 22 int u = S; 23 //从未用过的点中找到距离最小的点 24 for (int j = 1; j <= N; ++j) { 25 if (!s[j] && dist[j] < tmp_min) { 26 u = j; 27 tmp_min = dist[j]; 28 } 29 } 30 s[u] = true; 31 //更新距离 32 for (int j = 1; j <= N; ++j) { 33 if (!s[j] && m[u][j] < INT_MAX) { 34 dist[j] = min(dist[j], dist[u]+m[u][j]); 35 } 36 } 37 } 38 cout << dist[T] << endl; 39 } 40 41 int main() { 42 while (cin >> N >> M >> S >> T) { 43 int u, v, len; 44 for (int i = 1; i <= N; ++i) 45 m[i].assign(N+1, INT_MAX); 46 for (int i = 0; i < M; ++i) { 47 cin >> u >> v >> len; 48 m[u][v] = m[v][u] = min(m[u][v], len); 49 } 50 solve(); 51 } 52 return 0; 53 }
Dijkstra算法堆优化:
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 7 const int INF = 1e9; 8 9 struct edge { 10 int idx; 11 int dist; 12 edge(int _idx, int _dist) : idx(_idx), dist(_dist) {} 13 }; 14 15 struct cmp { 16 bool operator () (const edge &a, const edge &b) { return a.dist > b.dist; } 17 }; 18 19 int N, M, S, T; 20 vector<vector<edge>> graph; 21 22 void solve() { 23 priority_queue<edge, vector<edge>, cmp> heap; 24 vector<int> dist(N + 1, INF); 25 vector<bool> visit(N + 1, false); 26 dist[S] = 0; 27 visit[S] = true; 28 for (int i = 0; i < graph[S].size(); ++i) { 29 auto v = graph[S][i]; 30 dist[v.idx] = min(dist[v.idx], v.dist); 31 heap.push(edge(v.idx, dist[v.idx])); 32 } 33 while (!heap.empty()) { 34 auto u = heap.top(); 35 heap.pop(); 36 if (u.idx == T) { 37 cout << u.dist << endl; 38 return; 39 } 40 if (visit[u.idx]) continue; 41 visit[u.idx] = true; 42 for (int i = 0; i < graph[u.idx].size(); ++i) { 43 auto v = graph[u.idx][i]; 44 if (!visit[v.idx] && dist[v.idx] > dist[u.idx] + v.dist) { 45 dist[v.idx] = dist[u.idx] + v.dist; 46 heap.push(edge(v.idx, dist[v.idx])); 47 } 48 } 49 } 50 cout << "-1" << endl; 51 } 52 53 int main() { 54 while (cin >> N >> M >> S >> T) { 55 int u, v, len; 56 graph.resize(N + 1); 57 for (int i = 0; i < M; ++i) { 58 cin >> u >> v >> len; 59 graph[u].push_back(edge(v, len)); 60 graph[v].push_back(edge(u, len)); 61 } 62 solve(); 63 } 64 return 0; 65 }
SPFA算法:
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <algorithm> 5 #include <climits> 6 using namespace std; 7 8 const int INF = 1e9; 9 10 struct node { 11 int idx; 12 int len; 13 node(int _idx = 0, int _len = 0) : idx(_idx), len(_len){} 14 }; 15 16 int N, M, S, T; 17 vector<vector<node>> graph; 18 vector<int> dist; 19 20 void solve() { 21 vector<int> visit(N+1, false); 22 //priority_queue<int, vector<int>, greater<int>> heap; 23 queue<int> heap; 24 dist[S] = 0; 25 heap.push(S); 26 visit[S] = true; 27 while (!heap.empty()) { 28 //int u = heap.top(); 29 int u = heap.front(); 30 heap.pop(); 31 visit[u] = false; 32 for (int i = 0; i < graph[u].size(); ++i) { 33 int v = graph[u][i].idx; 34 if (dist[v] > dist[u] + graph[u][i].len) { 35 dist[v] = dist[u] + graph[u][i].len; 36 if (!visit[v]) { 37 heap.push(v); 38 visit[v] = true; 39 } 40 } 41 } 42 } 43 cout << dist[T] << endl; 44 } 45 46 int main() { 47 while (cin >> N >> M >> S >> T) { 48 graph.assign(N + 1, vector<node>()); 49 dist.assign(N + 1, INF); 50 int u, v, len; 51 for (int i = 1; i <= M; ++i) { 52 cin >> u >> v >> len; 53 graph[u].push_back(node(v, len)); 54 graph[v].push_back(node(u, len)); 55 } 56 solve(); 57 } 58 return 0; 59 }
Floyd算法:
#include <iostream> #include <vector> #include <climits> #include <algorithm> using namespace std; const int INF = 1e9; int N, M; vector<vector<int>> graph; void solve() { for (int k = 1; k <= N; ++k) { for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]); } } } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { cout << graph[i][j] << " "; } cout << endl; } } int main() { while (cin >> N >> M) { graph.assign(N+1, vector<int>(N+1, INF)); for (int i = 1; i <= N; ++i) { graph[i][i] = 0; } int u, v, len; for (int i = 1; i <= M; ++i) { cin >> u >> v >> len; graph[u][v] = graph[v][u] = min(graph[u][v], len); } solve(); } return 0; }