一些神奇的(优化)板子——来自Loi_black的博客
deque<int>q;
void spfa(int s)
{
for(int i=;i<=n;i++)
d[i]=1e9;
d[s]=;
q.push_back(s);
used[s]=;
while(!q.empty())
{
int x=q.front();
q.pop_front();
used[x]=;
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
if(d[u]>d[x]+hh[i].c)
{
d[u]=d[x]+hh[i].c;
if(!used[u])
{
used[u]=;
if(!q.empty())
{
if(d[u]<d[q.front()])
q.push_front(u);
else
q.push_back(u);
}
else
q.push_back(u);
}
}
}
}
}
//spfa+(slf优化)
int tim[maxn];
bool spfa(int s)
{
d[s]=;
q.push(s);
used[s]=;
while(!q.empty())
{
int x=q.front();
q.pop();
used[x]=;
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
if(d[u]>d[x]+hh[i].c)
{
d[u]=d[x]+hh[i].c;
if(!used[u])
{
if(++tim[u]>n)
return false;
q.push(u);
used[u]=;
}
}
}
}
return true;
} //spfa判负环
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
const int maxn=;
struct dqs
{
int f,t,c;
}hh[maxn];
struct dqm
{
int num,dis;
};
bool operator <(dqm a,dqm b)
{
return a.dis>b.dis;
}
int tot=,first[maxn],next[maxn],d[maxn];
bool used[maxn];
void build(int f,int t,int c)
{
hh[++tot]=(dqs){f,t,c};
next[tot]=first[f];
first[f]=tot;
}
priority_queue<dqm>q;
void dij(int s)
{
d[s]=;
q.push({s,d[s]});
while(!q.empty())
{
int head = q.top().num;
q.pop();
used[head]=;
for(int i=first[head];i;i=next[i])
{
int u=hh[i].t;
if(d[u]>d[head]+hh[i].c)
{
d[u]=d[head]+hh[i].c;
if(!used[u])
q.push((dqm){u,d[u]});
}
}
}
}
int main()
{
int n,m,s,e;
scanf("%d%d%d%d",&n,&m,&s,&e);
......
}
//dijkstra+ 堆
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
struct dqs
{
int f,t,c;
}hh[maxn<<];
int tot=,fa[maxn][],next[maxn],first[maxn],f[maxn],d[maxn];
void build(int ff,int tt,int cc)
{
hh[++tot]=(dqs){ff,tt,cc};
next[tot]=first[ff];
first[ff]=tot;
}
int deep[maxn];
void dfs(int x,int sd)
{
deep[x]=sd;
int u;
for(int i=first[x];i;i=next[i])
{
u=hh[i].t;
if(!deep[u]&&u)
{
f[u]=x;
d[u]=d[x]+hh[i].c;
dfs(u,sd+);
}
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y])
swap(x,y);
int deepcha=deep[x]-deep[y];
for(int i=;i<=;i++)
{
if(<<i&deepcha)
x=fa[x][i];
}
for(int i=;i>=;i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
}
if(x!=y)
return f[x];
return x;
}
int main()
{
int n;
scanf("%d",&n);
int u,v,c;
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&c);
build(u,v,c);
build(v,u,c);
}
dfs(,);
for(int i=;i<n;i++)
fa[i][]=f[i];
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
int xx=lca(u,v);
printf("%d\n",d[u]+d[v]-*d[xx]);
}
return ;
} //倍增lca
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
const int maxn=;
struct dqs
{
int f,t,c;
}hh[maxn];
int tot=,first[maxn],next[maxn],du[maxn];
void build(int f,int t)
{
hh[++tot]=(dqs){f,t};
next[tot]=first[f];
first[f]=tot;
}
queue<int>q;
void tp()
{
while(!q.empty())
{
int x=q.front();
q.pop();
printf("%d ",x);
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
du[u]--;
if(!du[u])
q.push(u);
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;
while(true)
{
scanf("%d",&x);
if(x==)
break;
build(i,x);
du[x]++;
}
}
for(int i=;i<=n;i++)
if(!du[i])
q.push(i);
tp();
return ;
}
//拓扑排序
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=;
struct dqs
{
int f,t;
}hh[maxn];
int tot=;
int first[maxn],next[maxn];
void build(int f,int t)
{
hh[++tot]=(dqs){f,t};
next[tot]=first[f];
first[f]=tot;
}
int dfn[maxn],low[maxn],stack[maxn],size[maxn],du[maxn],jlqlt[maxn];
bool in_stack[maxn];
int tot1=,cnt=,snum=;
void group(int x)
{
dfn[x]=low[x]=++tot1;
stack[++snum]=x;
in_stack[x]=;
for(int i=first[x];i;i=next[i])
{
int u=hh[i].t;
if(!dfn[u])
{
group(u);
low[x]=min(low[x],low[u]);
}
else if(in_stack[u])
low[x]=min(low[x],dfn[u]);
}
if(dfn[x]==low[x])
{
cnt++;
while(true)
{
jlqlt[stack[snum]]=cnt;
in_stack[stack[snum]]=;
size[cnt]++;
snum--;
if(stack[snum+]==x)
break;
}
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
build(a,b);
}
for(int i=;i<=n;i++)
if(!dfn[i])
group(i);
for(int i=;i<=n;i++)
for(int j=first[i];j;j=next[j])
{
int u=hh[j].t;
if(jlqlt[i]!=jlqlt[u])
du[jlqlt[i]]++;
}
int sum1=,sum2=,x;
for(int i=;i<=cnt;i++)
{
if(size[i]>)
sum1++;
if(!du[i])
{
sum2++;
x=i;
}
}
printf("%d\n",sum1);
if(sum2==&&size[x]!=)
{
for(int i=;i<=n;i++)
{
if(jlqlt[i]==x)
printf("%d ",i);
}
}
else
printf("-1\n");
return ;
} //trajan
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=;
long long tmp[maxn],a[maxn];
long long ans=;
void merge(int l,int mid,int r)
{
int i=l,j=mid+,k=l;
while(i<=mid&&j<=r)
{
if(a[i]>a[j])
{
tmp[k++]=a[j++];
ans+=mid+-i;
}
else
tmp[k++]=a[i++];
}
while(i<=mid)
tmp[k++]=a[i++];
while(j<=r)
tmp[k++]=a[j++];
for(int i=l;i<=r;i++)
a[i]=tmp[i];
}
void merge_sort(int l,int r)
{
if(l<r)
{
int mid=(l+r)>>;
merge_sort(l,mid);
merge_sort(mid+,r);
merge(l,mid,r);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
merge_sort(,n);
for(int i=;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
printf("%lld",ans);
} //归并排序
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=;
int heap[maxn],cnt=;
void push(int x)
{
cnt++;
int now=cnt;
heap[now]=x;
while(now>)
{
if(heap[now]<heap[now/])
{
swap(heap[now],heap[now/]);
now/=;
}
else break;
}
}
void pop()
{
heap[]=heap[cnt];
int now=;
while(now*+<=cnt)
{
int l=now*,r=now*+;
if(heap[l]<heap[now])
{
if(heap[r]<heap[l])
swap(l,r);
swap(heap[l],heap[now]);
now=l;
}
else if(heap[r]<heap[now])
{
swap(heap[r],heap[now]);
now=r;
}
else break;
}
cnt--;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
push(x);
}
for(int i=;i<=n;i++)
{
printf("%d ",heap[]);
pop();
}
} //手打最小堆(最大堆同理)
一些神奇的(优化)板子——来自Loi_black的博客的更多相关文章
- 转载来自朱小厮博客的 一文看懂Kafka消息格式的演变
转载来自朱小厮博客的 一文看懂Kafka消息格式的演变 ✎摘要 对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在 ...
- 从JavaScript 数组去重看兼容性有关问题,及性能优化(摘自玉伯博客)
JavaScript 数组去重经常出现在前端招聘的笔试题里,比如: 有数组 var arr = ['a', 'b', 'c', '1', 0, 'c', 1, '', 1, 0],请用 JavaScr ...
- WordPress 性能优化:为什么我的博客比你的快
WordPress 很慢? 很多博主都会感觉 WordPress 很慢?作为全世界最常用的建站和博客系统 WordPress 来说,在性能设计上肯定不会有太大的问题,WordPress 开发团队也肯定 ...
- 数论板子——来自Loi_black
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> usin ...
- 来自MarsEdit的博客测试
使用MarsEdit编辑的第一个测试博客. 希望我们一帆风顺! 插图,在插图时可以调整尺寸: 六种公式写法,记得要在选项中打开-启用数学公式: \begin{equation}\sum\end{e ...
- 一句话为当前窗口客户区捉图: GetFormImage 来自万一的博客
一句话为当前窗口客户区捉图: GetFormImage http://www.cnblogs.com/del/archive/2008/10/24/1318738.html unit Unit1; i ...
- ASP.NET中常用输出JS脚本的类(来自于周公博客)
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- wdcp 打开网页显示 Apache 2 Test Page powered by CentOS -- 来自辉哥博客
是因为更新过系统,安装并更新了系统自带的apache 执行这个命令即可 #ln -sf /www/wdlinux/init.d/httpd /etc/rc.d/init.d/httpd#reboot ...
- VuePress 博客之 SEO 优化(三)标题、链接优化
前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 本篇讲讲 ...
随机推荐
- C/C++ 关键字的使用方法详解
cppreference.com -> C/C++ 关键字 -> 细节 C/C++ 关键字 asm 语法: asm( "instruction" ); asm允许你在你 ...
- Hadoop学习基础之三:MapReduce
现在是讨论这个问题的不错的时机,因为最近媒体上到处充斥着新的革命所谓“云计算”的信息.这种模式需要利用大量的(低端)处理器并行工作来解决计算问题.实际上,这建议利用大量的低端处理器来构建数据中心,而不 ...
- python更新模块
pip install -U 模块名 # 这是 python2+ 版本的用法更新模块 pip3 install -U 模块名 # 这是 python3+ 版本的用法更新模块
- php备份mysql数据库
<?php /*程序功能:mysql数据库备份功能*/ ini_set('max_execution_time','0'); ini_set('memory_limit','1024M');// ...
- win10系统修改Intel VT-x时进入不了BIOS问题
一般电脑进入BIOS的方式都是在开机的时候不停的按F2或者F12,但是Win10系统由于支持快速启动,当win10系统快速启动的时候,按F12或者F2是没反应的,解决方式: 第一步:修改win10的启 ...
- 【leetcode刷题笔记】4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- nand flash详解及驱动编写
https://www.crifan.com/files/doc/docbook/linux_nand_driver/release/html/linux_nand_driver.html#nand_ ...
- ScreenOS地址转换
目录 1. NAT-src 1.1 来自DIP池(启用PAT)的NAT-src 1.2 来自DIP池(禁用PAT)的NAT-src 1.3 来自DIP池(带有地址变换)的NAT-src 1.4 来自出 ...
- Python 元组Tuple概念和操作
# 元组概念:有序的不可变的元素集合 # 和列表的区别就是, 元组元素不能修改 # 定义 # 一个元素的写法 # (666,) t = (666,) #正确写法 t = (666) #错误写法,括号当 ...
- Linux mysql主从同步配置
一.在两台Ubuntu机器上安装mysql1.检查系统中是否安装了mysql 这个是已经安装了的 没有安装的话执行上条命令===============================MySQL的一些 ...