洛谷P4779 【模板】单源最短路径(标准版)

P4779 【模板】单源最短路径(标准版) $Dijkstra$ 模板,求解单源最短路,复杂度 $O(m \log(n))$,其中 $n$ 为点数,$m$ 为边数。 思想是把已经求出最短距离的点加入到集合中,然后把其它点到起点的距离加入到最小堆中,每次从堆顶取出元素 $i$,用它去做松弛操作,如果能够更新相邻点 $j$,那么把 $j$ 的距离加入到最小堆中。可以保证每次从堆顶取出元素之后,这个元素的最短距离就已经被确定,可以直接把它加入到集合中。 算法要求边权值都是正数。 // Date: Fri Dec 29 11:33:23 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nemp(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif const int N = 100100, M = 200100; int n, m, s, h[N], st[N], dis[N]; int idx, e[M], ne[M], w[M]; void Init() { idx = 0; memset(h, -1, sizeof h); memset(st, 0, sizeof st); memset(dis, 0x3f, sizeof dis); } void Add(int a, int b, int c) { e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++; } void dijkstra(int s) { dis[s] = 0; priority_queue<PII, vector<PII>, greater<PII>> q; q.push({0, s}); while (nemp(q)) { auto t = q.top(); q.pop(); int base = t.f1, ver = t.f2; if (st[ver]) continue; st[ver] = true; ForE(i, ver) { int j = e[i], tmp = base + w[i]; if (tmp < dis[j]) { dis[j] = tmp; q.push({tmp, j}); } } } } int main(void) { #ifdef _DEBUG freopen("4779.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> s; Init(); For1(i, 1, m) { int a, b, c; cin >> a >> b >> c; Add(a, b, c); } dijkstra(s); For1(i, 1, n) { int tmp = dis[i]; if (tmp == INF) cout << ((1LL << 31) - 1) << ' '; else cout << tmp << ' '; } cout << '\n'; return 0; }

December 29, 2023 · 3 min · 537 words

CodeForces 1915G Bicycles

Bicycles tutorial 点的数量 $n \le 1000$,边的数量 $m \le 2000$,然后再记录一下到达每个点的时候的速度 $s_i$,而 $s_i \le 1000$,因此点数最多有 $10^6$ 个。然后使用 $Dijkstra$ 得到 $dis[n][s_i]$,其中 $1 \le s_i \le 1000$,在里面找到最小值。 // Date: Fri Dec 29 12:04:17 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ll INF = 0x3f3f3f3f'3f3f3f3f, MOD = 1e9 + 7; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nemp(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif using PLI = pair<ll, int>; const int N = 1100, M = 1100 * 2; int t, n, m, h[N], g[N][N], s[N]; int idx, e[M], ne[M], w[M]; struct point { ll val; int ver, s0; point(ll _val, int _ver, int _s0) : val(_val), ver(_ver), s0(_s0) {} bool operator>(const point &rhs) const { return val > rhs.val; } }; void Init() { idx = 0; memset(h, -1, sizeof h); memset(g, 0x3f, sizeof g); } void Add(int a, int b, int c) { e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++; } ll dijkstra() { priority_queue<point, vector<point>, greater<point>> q; q.push({0, 1, s[1]}); map<PII, ll> dis; dis[{1, s[1]}] = 0; set<PII> vis; while (nemp(q)) { auto t = q.top(); q.pop(); ll base = t.val; int ver = t.ver, sv = t.s0; if (has(vis, PII(ver, sv))) continue; vis.insert(PII(ver, sv)); ForE(i, ver) { int j = e[i], svj = min(sv, s[j]); PII pj = PII(j, svj); ll tmp = base + sv * w[i]; if (!has(dis, pj) || tmp < dis[pj]) { dis[pj] = tmp; q.push(point(tmp, j, svj)); } } } ll res = INF; For1(i, 1, 1000) { PII p = PII(n, i); if (dis.find(p) != dis.end()) { res = min(res, dis[p]); } } return res; } int main(void) { #ifdef _DEBUG freopen("g.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { Init(); cin >> n >> m; For(i, 0, m) { int a, b, c; cin >> a >> b >> c; Add(a, b, c); Add(b, a, c); } For1(i, 1, n) { cin >> s[i]; } cout << dijkstra() << '\n'; } return 0; }

December 29, 2023 · 3 min · 619 words