洛谷P3371 【模板】单源最短路径(弱化版)

P3371 【模板】单源最短路径(弱化版) SPFA 模板。大概思想是把松弛过的点加入队列里面,从队列取出一个点 $i$ 去松弛它的出边 $j$,如果 $j$ 的距离能够被更新,那么把 $j$ 加入到队列里面,它以后可能会更新相邻的点的距离。如果没有可以松弛的点,说明所有点的最短距离都已经得到。 要求不能有负环,但是可以有负权值边。复杂度是 $O(m)$,最坏情况 $O(nm)$,其中 $n$ 是点数,$m$ 是边数。 // Date: Mon Jan 1 18:26:54 2024 #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; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const ll INFL = 0x3f3f3f3f'3f3f3f3f; 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}, }; 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()) #define NL cout << '\n'; template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } 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; } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #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; #define dbg(x...) \ cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \ _print(x); \ cerr << "\e[39m" << endl; #else #define debug1 #define debug2 #define debug3 #define dbg(x...) #endif const int N = 10100, M = 500100; int n, m, s, idx, h[N], dis[N]; int e[M], ne[M], w[M]; bool st[N]; void Init() { idx = 0; memset(h, -1, sizeof h); memset(st, false, 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 spfa(int s) { dis[s] = 0; st[s] = true; queue<int> q({s}); while (nemp(q)) { auto ver = q.front(); q.pop(); st[ver] = false; ForE(i, ver) { int j = e[i]; if (ckmin(dis[j], dis[ver] + w[i]) && !st[j]) { st[j] = true; q.push(j); } } } } void solve() { cin >> n >> m >> s; Init(); For(i, 0, m) { int a, b, c; cin >> a >> b >> c; Add(a, b, c); } spfa(s); For1(i, 1, n) { if (dis[i] == INF) cout << (1LL << 31) - 1 << ' '; else cout << dis[i] << ' '; } cout << '\n'; } int main(void) { #ifdef _DEBUG freopen("3371.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; while (T--) { solve(); } return 0; }

January 1, 2024 · 4 min · 847 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

CodeForces 1676G White-Black Balanced Subtrees

White-Black Balanced Subtrees 一次 DFS 之后统计出各个节点子树的黑色节点和白色节点的个数。 // Date: Sat Dec 16 10:34:02 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 nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #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 = 4010, M = 10010; int h[N], w[N], b[N], t, n; int e[M], ne[M], idx; char color[N]; void Init() { idx = 0; memset(h, -1, sizeof h); memset(w, 0, sizeof w); memset(b, 0, sizeof b); } void Add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } void dfs(int x) { int w1 = 0, b1 = 0; for (int i = h[x]; i != -1; i = ne[i]) { int j = e[i]; dfs(j); w1 += w[j]; b1 += b[j]; } if (color[x] == 'B') b1++; else w1++; w[x] = w1; b[x] = b1; } int main(void) { #ifdef _DEBUG freopen("1676g.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n; Init(); For1(i, 2, n) { int j; cin >> j; Add(j, i); } memset(color, 0, sizeof color); string s; cin >> s; For(i, 0, SZ(s)) { color[i + 1] = s[i]; } dfs(1); int res{}; For1(i, 1, n) { if (w[i] == b[i]) res++; } cout << res << '\n'; } return 0; }

December 16, 2023 · 3 min · 457 words