2025年2月26日大约 4 分钟
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1010
题目大意
小狗要逃离一个的长方形迷宫,迷宫出口的门仅在第 秒时开一瞬间(不到1秒)。因此,小狗必须恰好在第 秒到达门口才能逃离。 每一秒,它可以向上下左右任意移动一格,且所有格子至多走一次。
2023年11月13日大约 3 分钟
题目传送门:https://www.luogu.com.cn/problem/P1807
解法一:拓扑排序
#include <bits/stdc++.h>
using i64 = long long;
using namespace std;
typedef pair<int, int> pii;
constexpr int N = 1505, M = 5e4 + 5;
// mt19937_64 rng(random_device{}());
int n, m;
int ver[M], edge[M], nxt[M], head[N], tot, in[N];
i64 d[N];
void add(int x, int y, int z) {
ver[++tot] = y, edge[tot] = z, nxt[tot] = head[x], head[x] = tot;
}
void topsort() {
queue<int> q;
for (int x = 2; x <= n; ++x) {
if (in[x] == 0) {
q.push(x);
}
}
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = head[x]; i ; i = nxt[i]) {
int y = ver[i];
if (--in[y] == 0) {
q.push(y);
}
}
}
d[1] = 0;
q.push(1);
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = head[x]; i ; i = nxt[i]) {
int y = ver[i], z = edge[i];
d[y] = max(d[y], d[x] + z);
if (--in[y] == 0) {
q.push(y);
}
}
}
}
const i64 inf = 1e18;
void solve() {
cin >> n >> m;
fill(d, d + 1 + n, -inf);
for (int i = 0; i < m; ++i) {
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
++in[y];
}
topsort();
cout << (d[n] ==-inf ? -1 : d[n]) << '\n';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
2025年6月9日大约 2 分钟
题目传送门:https://www.luogu.com.cn/problem/P1462
第一感觉跟leetcode做过的这道题很像,于是采用启发式搜索,取h(x) 恒等于0,很显然启发函数是可接受的、一致的,即Dijkstra。
2025年4月22日大约 3 分钟
2025年4月21日小于 1 分钟
2025年4月12日小于 1 分钟
排序算法总览
排序算法 | 平均时间复杂度 | 最好 | 最坏 | 空间复杂度 | 稳定性 |
---|---|---|---|---|---|
冒泡排序 | O(n^2) | O(n) | O(n^2) | O(1) | 稳定 |
选择排序 | O(n^2) | O(n^2) | O(n^2) | O(1) | 不稳定 |
插入排序 | O(n^2) | O(n) | O(n^2) | O(1) | 稳定 |
希尔排序 | O(nlog²n) | O(nlog²n) | O(nlog²n) | O(1) | 不稳定 |
归并排序 | O(nlogn) | O(nlogn) | O(nlogn) | O(n) | 稳定 |
快速排序 | O(nlogn) | O(nlogn) | O(n^2) | O(logn) | 不稳定 |
堆排序 | O(nlogn) | O(nlogn) | O(nlogn) | O(1) | 不稳定 |
计数排序 | O(n+m) | O(n+m) | O(n+m) | O(m) | 稳定 |
桶排序 | O(n) | O(n) | O(n^2) | O(n) | 稳定 |
基数排序 | O(n·k) | O(n·k) | O(n·k) | O(n+k) | 稳定 |
2025年2月24日大约 3 分钟
2023年12月27日小于 1 分钟
2023年10月17日小于 1 分钟