20 行代码C++算法模板
埃氏筛
埃氏筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N log log N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
#质数#质数筛
埃氏筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N log log N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
BFS 搜索无权图中从起点到终点的最短路径长度(邻接矩阵)
#include <bits/stdc++.h>
using namespace std;
const int N = 105; // 根据题目点的数量范围调整
int g[N][N]; // 邻接矩阵,g[u][v] = 1 表示有边
bool vis[N];DFS 搜索无权图中从起点到终点的简单路径数量(邻接矩阵)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
int g[N][N]; // 邻接矩阵,g[u][v] = 1 表示有边
bool vis[N];BFS 搜索无权图中从起点到终点的最短路径长度(邻接表)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
vector<int> adj[N]; // 邻接表(有权图需要使用结构体)
bool vis[N];DFS 搜索无权图中从起点到终点的简单路径数量(邻接表)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
vector<int> adj[N]; // 邻接表(有权图需要使用结构体)
bool vis[N];BFS 搜索二维矩阵中从 (1, 1) 到 (n, m) 的最短路径长度
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};