- 博客(25)
- 收藏
- 关注
原创 【图论】【Dijistra】优先队列实现
时间复杂度为mlogn #include #include #include #include using namespace std; const int N=1000; const int INF = 0x3f3f3f3f; struct node { int to; int v; node(int t, int o) :to(t), v(o) {} }; struct cmp { bool operator()(node a, node b) { return a.v > b.v; } }
2022-02-09 18:30:40
489
原创 【图论】【Prim算法】优先队列实现
时间复杂度O(mlogn) 计算带权无向连通图G的最小生成树。 输入格式: 第一行两个整数:N(1≤N≤300000),表示结点集{1,2,3,⋯,N};M(0≤M≤600000)表示边的条数。 接下来M行,每行表示一条带权的边,用3个整数u,v,c表示,分别表示一条边的两个端点以及其权值(权值范围0≤c≤10 9 )。 输出格式: 一个整数,表示G的最小生成树的边权之和。 #include #include #include #include using namespace std; const int
2022-02-09 17:18:21
1009
原创 【动态规划】经典例题
#include using namespace std; void feibonaqi()//斐波那契数列 { int n; cin >> n; int bp[100]; bp[1] = bp[2] = 1; for (int i = 3; i <=n; i++) { bp[i] = bp[i - 1] + bp[i - 2]; } cout << bp[n] << endl; } void maxsum()//最大子序和 { int a[7] =
2022-01-24 16:38:21
338
原创 【动态规划】01背包问题
题目描述:给定若干物品,它们有一定的重量和价值,以及一个有一定容量上限的背包,物品每种类型只有一个且在装包时只可以选择装或者不装,不能装一部分。求背包所能容纳的最大价值。 #include using namespace std; int main() { int w[4] = { 0,3,4,5 }; int v[4] = { 0,4,5,6 }; int m = 8; int n = 3; int max = 0; int dp[4][9]; for (int i = 0; i < 4;
2022-01-24 14:12:22
291
原创 【算法导论】【随机二叉搜索树】
我们已经证明了二叉搜索树上的每个基本操作都能在O ( h ) O(h)O(h)时间内完成,其中h hh为这棵树的高度。然而,随着元素的插入和删除,二叉搜索树的高度是变化的。例如,如果n nn个关键字按严格递增的次序被插入,则这棵树一定是高度为n − 1 n-1n−1的一条链。 遗憾的是,当一棵二叉搜索树同时由插入和删除操作生成时,我们对这棵树的平均高度了解的甚少。当树是由插入操作单独生成时,分析就会变得容易得多。因此,我们定义n nn个关键字的一棵随机构建二叉搜索树为按随机次序插入这些关键字到棵初始的空树中
2022-01-14 22:13:10
660
原创 【平衡树】二叉树
#include #include #include<stdlib.h> #include<time.h> #include #include using namespace std; typedef struct node* tree; struct node { long int data; node* left; node* right; }; int GetHeight(tree root) { int Hr, Hl, m; if (!root) { return 0;
2022-01-13 18:47:14
128
原创 二叉树三种建树方式【普通】【前序】【完美】
#include #include #include<stdlib.h> #include<time.h> #include #include using namespace std; typedef struct node* tree; struct node { long int data; node* left; node* right; }; tree buildtree1()//普通建树,就是根据已知将各个位置填写 { tree root = new node; roo
2022-01-13 15:06:02
356
原创 【算法导论】【分治】【递归】快速排序
根据算法导论中给出的思想用分治递归进行实现的快速排序 #include #include #include<stdlib.h> #include<time.h> #include using namespace std; #define random(x)(rand()%x) int aa[10] = { 1,3,4,5,7,9,2,6,0,8 }; int quitsort(int begin1, int end1)//就地调正 { if (begin1 < end1)
2022-01-12 16:02:38
239
原创 完全哈希C++实现
完全哈希 基本思想:完全哈希采用二级结构,每一级上都采用 全域散列。其实犹如用链表解决碰撞的哈希表那样,只不过完全哈希不是使用链表而是使用一 个小的哈希表来实现,且保证在小的哈希表上不发生碰撞 第一级按照正常全域哈希即可,第二级哈希表大小由此处碰撞个数n_i决定,m_i = n_i^2,碰撞个数要缩减到最小,否则造成极大浪费。 第一级哈希的槽中储存着二级哈希函数,二级也与一级类似。 中心思想就是在第一次的时候先找到元素要插入的地方的开头,然后将开头带回来,再去插入。 #include #include
2022-01-11 17:57:31
623
原创 全域哈希C++实现
全域哈希代码的写法不唯一但是中心思想就是在构建插入时进行随机的使用哈希函数进行插入,一个元素投射到哈希表相同位置的函数有|H|/m个,|H|为集合中函数总个数,m为槽数 #include #include #include<stdlib.h> #include<time.h> using namespace std; #define random(x)(rand()%x) const int m = 16; const int n = 13; typedef int KeyTy
2022-01-11 15:43:24
569
原创 【分治】【递归】求最接近元素
#include #include using namespace std; vectorvec; int fen(int begin1, int end1, int k) { if (begin1 < end1) { int mid = (begin1 + end1) / 2; int kk; if (vec[mid] > k) { kk = fen(begin1, mid-1, k); if (kk != -1) { return kk; }else
2022-01-10 18:46:28
279
原创 图论--Dijkstra算法
#include #include #include #include #include #define INF 0x3f3f3f3f using namespace std; const int maxn = 10005; int a[maxn][maxn]; int vis[maxn], dist[maxn]; long long n, m; int u, v, w; long long sum = 0; int Dijkstra(int s) { dist[s] = 0; for (int
2021-12-07 20:16:04
153
原创 图论--打时间戳
#include #include #include #include #include using namespace std; int b = 0, end; int map[50][50]; int d[10005]; int v[10005]; int vis[50]; int n; void DFS(int x) { vis[x] = 1; d[x] = b; for (int i = 0; i < n; i++) { if (map[x][i] && !vis
2021-12-07 11:22:59
226
原创 图论-强连通分量
#include #include #include #include using namespace std; vector<vector> a; vector vis; int n,m; stack sta; void DFS(int x) { if (vis[x]==1) { return ; } vis[x]=1; for (int i : a[x]) { DFS(i); } } int main() { cin>>n>>m; vis.resi
2021-11-28 13:42:51
623
原创 图论-关键路径
#include #include #include #include #include using namespace std; vector<vector> vec; vector vis,dur,et,ft,path={-1}; int n,m,x,y; stack sta; void topuDFS(int e) { vis[e]=1; for (auto t : vec[e]) { if(!vis[t]) { topuDFS(t); } if(ft[t]>=et
2021-11-11 12:54:37
137
原创 前序中序,后续中序求树
#incldue<bits/stdc++.h> using namespace std; struct node { int date; node * left; node * right; }; strutc node * tree; int a[10005],b[10005]; tree bintree(int prel,int prer,int inl,int inr) { if(prel>prer) { return NULL; } tree root=new node; root
2021-11-11 11:58:40
230
原创 图论-拓扑排序
拓扑排序 #include #include #include #include using namespace std; vector<vector> vec; vector vis; int n,m,x,y; stack sta; void topuDFS(int e) { vis[e]=1; for (auto t : vec[e]) { if(!vis[t]) { topuDFS(t); } } sta.push(e); } int main () { cin>>n&
2021-11-11 11:55:31
102
原创 深搜与广搜
#include #include #include #include #include using namespace std; int mp[20][20], n, e,vis[20]; /* cin>>n>>e; while(e--) { int a,b; cin>>a>>b; mp[a][b]=mp[b][a]=1; } memset(vis,0,sizeof(vis)); mp数组全部为1,而vis数组全为0; mp数组用来指“路”,而vis
2021-10-26 19:19:39
183
原创 与树有关的代码
#include #include #include #include #include using namespace std; typedef struct node* tree; struct node { int data; tree left; tree right; }; int n=10; int a[10000], b[10000]; tree bintree(int prel, int prer, int inl,int inr) { //(int postl,int postr,i
2021-10-25 22:53:38
183
原创 时间复杂度
常数阶O(1) 无论代码执行了多少行,只要是没有循环等复杂结构,那这个代码的时间复杂度就都是O(1),如: int i = 1; int j = 2; ++i; j++; int m = i + j; 线性阶O(n) 这个在最开始的代码示例中就讲解过了,如: for(i=1; i<=n; ++i) { j = i; j++; } 对数阶O(logN) 还是先来看代码: int i = 1; while(i<n) { i = i * 2; } 从上面代码可以看到,在while循环里面,每次都将 i
2021-05-27 20:13:57
306
原创 STL c++
vector vector push_back(ele); //尾部插入元素ele pop_back(); //删除最后一个元素 insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele insert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素ele erase(const_iterator pos); //删除迭代器指向的元素 erase(const_iterator start, co
2021-05-27 20:09:59
131
原创 二叉搜索树
搜索树的建立与插入 struct node{ int data; node *left,*right; }; void insert(node* &root,int data){ if(root==NULL){ root=new node; root->data=data; root->left=root->right=NULL; return ; } if(datadata) insert(root->left,data); else insert(root->rig
2021-05-27 19:36:40
90
原创 二叉树求序列
structnode{ intdate; node*left; node*right; }; strutcnode*tree; inta[10005],b[10005]; 根据前序中序求后续 treebintree(intprel,intprer,intinl,intinr) { if(prel>prer) { returnNULL; } ...
2021-05-26 21:23:50
398
原创 平衡二叉树建立
平衡二叉树 struct node { int date; node * left; node * right; }; strutc node * tree; ## 算树高 int getheight(tree root) { int rh,lh,maxh; if(root==NULL) {return 0;} else { rh=getheight(root->right); hl=getheight(root->left); maxh=rh>lh?rh:lh; return maxh+
2021-05-26 20:58:35
151
原创 并查集操作
并查集 int a[10005]; 初始化 for(int i=1;i<=10005;i++) { a[i]=i; } 查找 ## 非递归 int find(int x) { while(x!=a[x]) { x=a[x]; } return x; } ## 递归 int find(int x) { if(x==a[x]) { return x; } else return find(a[x]); } 一般合并 void union(int x,int y) { if(find(x)!=find(y)
2021-05-25 23:22:42
144
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅