一、堆
掌握堆的向下调整and向上调整
#include <bits/stdc++.h>
using namespace std;
const int maxn=100;
int heap[maxn];
void downAdjust(int low, int high) //调整为最大堆 low为欲调整结点的数组下标 high一般为堆的最后一个元素的数组下标
{
int i=low,j=i*2;
while(j<=high) //存在孩子结点
{
if(j+1<=high&&heap[j+1]>heap[j]) //存在右孩子,并且右孩子>左孩子
{
j=j+1;
}
if(heap[j]>heap[i])
{
swap(heap[j],heap[i]);
i=j;//i是欲调整结点、j为i的左孩子
j=i*2;
}
else
{
break;
}
}
}
void createHeap(int n) //序列中元素个数为n
{
for(int i=n/2;i>=1;i--)
{
downAdjust(i,n);
}
}
void deleteTop(int n)
{
heap[1]=heap[n--];
downAdjust(1,n);
}
void upAdjust(int low,int high)
{
int i=high;
int j=i/2;
while(j>=low)
{
if(heap[j]<heap[i])
{
swap(heap[j],heap[i]);
i=j;
j=i/2;
}
else break;
}
}
void insert(int x,int n)
{
heap[++n]=x;
upAdjust(1,n); //向上调整新加入的结点
}
//堆排序
void heapSort(int n)
{
createHeap(n);
for(int i=n;i>1;i--)
{
printf("%d",heap[1]);
swap(heap[i],heap[1]);
downAdjust(1,i-1);
}
}
二、哈夫曼树
带权路径长度 WPL
计算带权路径长度——哈夫曼树的构建思想:反复选择两个最小的元素,合并,直到只剩下一个元素。 我们可以用优先队列(也就是最小堆)来执行这种策略
XDOJ——树中就有这样一道题
哈夫曼树 时间限制: 1 秒 内存限制: 256KB
问题描述
假设用于通信的电文由n个字符组成,字符在电文中出现的频度(权值)为w1,w2,…,wn,试根据该权值序列构造哈夫曼树,并计算该树的带权路径长度。输入说明 第1行为n的值,第2行为n个整数,表示字符的出现频度。
输出说明 输出所构造哈夫曼树的带权路径长度。
输入样例 8
7 19 2 6 32 3 21 10输出样例 261
#include<stdio.h>
#include<queue>
#include<functional>
using namespace std;
priority_queue<int ,vector<int> ,greater<int> > q;//最小堆 第三个参数 less<int> 最大堆
int main()
{
int n,wpl=0,i,t[1000];
int a,b;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&t[i]);
q.push(t[i]);
}
while(q.size()>=2)
{
a=q.top();q.pop();
b=q.top();q.pop();
q.push(a+b);
wpl=wpl+a+b;
}
//author:kkzzjx
printf("%d",wpl);
}
哈夫曼编码
哈夫曼编码是能使给定字符串编码成01串后长度最短的前缀编码
三、图的存储
1. 邻接矩阵
2. 邻接表
可以借助vector可变长容器
#include<bits/stdc++.h>
using namespace std;
const int N=100;
struct Node
{
int v;//边的终点编号
int w;//权重
Node(int _v,int _w):v(_v),w(_w){}
};
vector<Node> Adj[N];
int main()
{
Adj[1].push_back(Node(3,4));
}
四、图的遍历
DFS
设置一个vis[N] 记录有无访问过这个结点
#include<bits/stdc++.h>
using namespace std;
const int maxv=100;
const int INF=1000000;
//邻接矩阵版
int n,G[maxv][maxv]; //n为顶点数
bool vis[maxv]={false};
void DFS(int u,int depth)
{
vis[u]=true;
for(int v=0;v<n;v++)
{
if(!vis[v]&&G[u][v]!=INF)
{
DFS(v,depth+1);
}
}
}
void DFStrave()
{
for(int u=0;u<n;u++)
{
if(vis[u]==false)
{
DFS(u,1);
}
}
}
//邻接表版
vector<int> Adj[maxv];
//int n;
//vis[maxv]={false};
void DFS2(int u,int depth)
{
vis[u]=true;
for(int i=0;i<Adj[u].size();i++)
{
int v=Adj[u][i];
if(!vis[v])
{
DFS2(v,depth+1);
}
}
}
void DFSTrave()
{
for(int i=0;i<n;i++)
{
if(vis[i]==false)
{
DFS2(i,1);
}
}
}
BFS
利用数组inq[N]来记录结点有无入队
#include<bits/stdc++.h>
using namespace std;
const int maxv=100;
const int INF=100000;
int n,G[maxv][maxv];
bool inq[maxv]={false};
void BFS(int u)
{
queue<int> q;
q.push(u);
inq[u]=true;
while(!q.empty())
{
u=q.front();
q.pop();
for(int v=0;v<n;v++)
{
if(G[u][v]!=INF&&inq[v]==false)
{
q.push(v);
inq[v]=true;
}
}
}
}
vector<int> Adj[maxv];
//int n;
//bool inq[maxv]={false};
void BFS2(int u)
{
queue<int> q;
q.push(u);
inq[u]=true;
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=0;i<Adj[u].size();i++)
{
int v=Adj[u][i];
if(inq[i]==false)
{
q.push(v);
inq[v]=true;
}
}
}
}