最短路,dijkstra算法
AC代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #include<bits/stdc++.h> // #include <iostream> // #include <cstdio> // #include <queue> // #include <deque> // #include <stack> // #include <string> // #include <cstring> // #include <numeric> // #include <functional> // #include <cstdlib> // #include <vector> // #include <set> // #include <map> // #include <algorithm> // #include <cmath> // #include <iomanip> using i64 = long long; using namespace std; #define lowbit(x) ((x) & -(x)) #define endl '\n' #define IOS1 ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define IOS2 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; typedef long long ll; // typedef long long i64; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T power1(T a, int b) { T res = 1; for (; b; b >>= 1, a = a * a) { if (b & 1) { res = res * a; } } return res; } template<class T> T power2(T a, int b, T c) { T res = 1; for (; b; b >>= 1, a = a * a % c) { if (b & 1) { res = res * a % c; } } return res; } template <typename T> T Myabs(T a) { return a >= 0 ? a : -a; } template <typename T> inline void read(T& x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0', ch = getchar(); } x *= f; } const int INF = 0x3f3f3f3f; const int mod = 1000000007; // const int mod = 998244353; const double PI = acos(-1.0); const double eps = 1e-6; inline int sgn(double x) { return x < -eps ? -1 : x > eps; } /* Tips: 1.int? long long? 2.don't submit wrong answer 3.figure out logic first, then start writing please 4.know about the range 5.check if you have to input t or not 6.modulo of negative numbers is not a%b, it is a%b + abs(b) */ int n, m; struct node { int to, w; node (int a, int b) { to = a; w = b; } }; vector<node> e[1010]; i64 dis[1010], ans[1010], res = -1; void dijkstra(int x) { for (int i = 1; i <= n; i++) { dis[i] = INF; } dis[x] = 0; priority_queue<pair<int, int>> q; q.push({0, x}); while (!q.empty()) { pair<int, int> p = q.top(); q.pop(); for (int i = 0; i < e[p.second].size(); i++) { int t = e[p.second][i].to; if (dis[t] > dis[p.second] + e[p.second][i].w) { dis[t] = dis[p.second] + e[p.second][i].w; q.push({-dis[t], t}); } } } int cnt = 0; for (int i = 1; i <= n; i++) { if (dis[i] != INF) { ans[++cnt] = dis[i]; } } if (cnt > 2) { sort(ans + 1, ans + 1 + cnt); res = max(res, ans[cnt] + ans[cnt - 1]); } } void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) { e[i].clear(); } for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; e[a].push_back(node(b, c)); e[b].push_back(node(a, c)); } res = -1; for (int i = 1; i <= n; i++) { dijkstra(i); } cout << res << endl; return; } int main() { IOS1; //IOS2; #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int __t = 1; cin >> __t; for (int _t = 1; _t <= __t; _t++) { solve(); } return 0; } /* */
spfa
AC代码:
#include<iostream> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int N = 2010; int h[N], e[N], ne[N], w[N], idx, dist[N]; bool st[N]; int n, m, res = -1; void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++; } int spfa(int x) { memset(st, false, sizeof st); memset(dist, 0x3f, sizeof dist); dist[x] = 0; queue<int> q; q.push(x); st[x] = true; while(!q.empty()) { int t = q.front(); q.pop(); st[t] = false; for(int i = h[t]; i != -1; i = ne[i]) { int j = e[i]; if(dist[j] > dist[t] + w[i]) { dist[j] = dist[t] + w[i]; if(!st[j]) { q.push(j); st[j] = true; } } } } int max1 = -1, max2 = -1; dist[x] = -2; for(int i = 1; i <= n; i++) { if(dist[i] == 0x3f3f3f3f)continue; else if(dist[i] > max1)max2 = max1, max1 = dist[i]; else if(dist[i] > max2)max2 = dist[i]; } if(max1 != -1 && max2 != -1)return max1 + max2; else return -1; } int main() { int t; cin >> t; while(t--) { cin >> n >> m; memset(h, -1, sizeof h); memset(e, 0, sizeof e); memset(ne, 0, sizeof ne); memset(w, 0, sizeof w); idx = 0; res = -1; while(m--) { int a, b, c; cin >> a >> b >> c; add(a, b, c); add(b, a, c); } for(int i = 1; i <= n; i++) res = max(res, spfa(i)); cout << res << endl; } return 0; }
旅行(最短路)(dijkstra算法)
最新推荐文章于 2022-12-31 10:11:16 发布