一些神奇的(优化)板子——来自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 中文文档. 本篇讲讲 ...
随机推荐
- 使用git工具上传项目到github步骤
这里记录一下上传项目到github的步骤.使用的工具是Git bash. 1.登陆github,没有账户就注册一个,新建一个Repository(仓库). 2.绑定用户. 因为Git是分布式版本控制系 ...
- CentOS Linux中zip压缩和unzip解压缩命令详解
以下命令均在/home目录下操作cd /home #进入/home目录1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip myda ...
- 前端基础之jquery练习
实例练习 左侧菜单 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- JAVA中的Token 基于Token的身份验证
最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求的SessionId,导致服务器每次接收安卓发送的请求访问时都 ...
- ubuntu里设置从串口登录
1) Create a file called /etc/init/ttyS0.conf containing the following: # ttySAC0 - getty # # This se ...
- 使用ASP.Net MVC5 Web API OData和Sencha Touch 开发WebAPP
使用ASP.Net MVC5 Web API OData和SenCha Touch 开发WebAPP Demo 效果 第一步 创建数据库 创建表 第二步 搭建MVC,并导入OData 第三步,写入We ...
- ucsc genome brower的用法和说明(一)
官网说明书:http://genome.ucsc.edu/goldenpath/help/hgTracksHelp.html 1.genome brower的作用 a,展示任何尺度的基因组片段.比如, ...
- Go HelloWorld 网络版和并发版
网络版 package main import ( "net/http" "fmt" ) func main() { http.HandleFunc(" ...
- gitblit搭建git服务器
如果你的公司使用git作为版本管理工具,那么对gitblit应该也不会陌生.gitblit是一个开源的git服务器java实现,一般情况下gitblit都是由别人已经搭建好你直接使用就行了,除非你就是 ...
- JMeter接口测试报错,反馈和postman不一样(二)
我总共现在有两个可以学习的接口,昨天测试一个接口发现问题解决后,今天测试另外一个发现又有问题了 这一次还是反馈显示不一样 要么 这种情况是直接从postman里面拿过来的数据,没做处理 报not j ...