首页 > 其他分享 >模板集

模板集

时间:2024-11-22 09:56:07浏览次数:1  
标签:GCC ch int pragma optimize 模板 define

前言:

洛谷博客园CSDN 同步更新

博客园 可能食用更佳

Part 0 缺省源 & 卡常

Part 0.1 火车头

#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(2)

Part 0.2 宏定义

#define re register
#define il inline
#define ls u<<1
#define rs u<<1|1
#define lowbit(x) (x&-x)
#define PII pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define clear(x) memset(x,0,sizeof(x))
#define ll long long
#define ld long double
#define pi acos(-1.0)

Part 0.3 快读快写

#define il inline
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?0:*p1++)
char buf[1<<20],*p1,*p2;
template <typename T>
il void read(T &x)
{
	x=0;int f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	x*=f;
}
template <typename T>
il void write(T x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+48);
}
il char getc()
{
	char ch=getchar();
	while(ch=='\n'||ch=='\r'||ch==' ') ch=getchar();
	return ch;
}

Part 1 最短路

Part 1.1 Floyd

B3647 【模板】Floyd

时间复杂度 \(\mathcal O(n^3)\)

#include<bits/stdc++.h>
#define il inline
using namespace std;
const int N=105;
int n,m,dis[N][N];
template <typename T>
il void read(T &x)
{
	x=0;int f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	x*=f;
}
void Floyd()
{
	for(int k=1;k<=n;k++)
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				if(i^j&&i^k&&j^k&&dis[i][k])
					dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
int main()
{
	read(n),read(m);
	memset(dis,0x3f,sizeof dis);
	for(int i=1;i<=n;i++) dis[i][i]=0;
	for(int i=1,u,v,w;i<=m;i++)
	{
		read(u),read(v),read(w);
		dis[u][v]=dis[v][u]=min(dis[u][v],w);
	}
	Floyd();
	for(int i=1;i<=n;i++,puts(" "))
		for(int j=1;j<=n;j++)
			printf("%d ",dis[i][j]);
	return 0;
}

Part 1.2 Dijkstra

1.2.1 无优化 Dijkstra

P3371 【模板】单源最短路径(弱化版)

时间复杂度 \(\mathcal O(n^2)\)

#include<bits/stdc++.h>
#define il inline
#define pb push_back
using namespace std;
const int N=1e4+5,INF=2147483647;
int n,m,s,dis[N];
bool vis[N];
struct Edge
{
	int v,w;
};
vector<Edge> g[N];
template <typename T>
il void read(T &x)
{
	x=0;int f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	x*=f;
}
void Dijkstra(int s)
{
	for(int i=1;i<=n;i++) dis[i]=INF;
	dis[s]=0;
	for(int i=1;i<n;i++)
	{
		int mindis=INF,u;
		for(int j=1;j<=n;j++)
			if(!vis[j]&&mindis>dis[j])
				mindis=dis[j],u=j;
		vis[u]=1;
		for(auto [v,w]:g[u])
			if(!vis[v]&&dis[v]>mindis+w)
				dis[v]=mindis+w;
	}
}
int main()
{
	read(n),read(m),read(s);
	for(int i=1,u,v,w;i<=m;i++)
	{
		read(u),read(v),read(w);
		g[u].pb((Edge){v,w});
	}
	Dijkstra(s);
	for(int i=1;i<=n;i++) printf("%d ",dis[i]);
	return 0;
}

1.2.2 堆优化 Dijkstra

P4779 【模板】单源最短路径(标准版)

时间复杂度 \(\mathcal O((m+n)\log n)\)

#include<bits/stdc++.h>
#define il inline
#define PII pair<int,int>
#define mp make_pair
#define fi fisrt
#define se second
#define pb push_back
using namespace std;
const int N=1e5+5;
int n,m,s,dis[N];
bool vis[N];
priority_queue<PII,vector<PII>,greater<PII>> q;
struct Edge
{
	int v,w;
};
vector<Edge> g[N];
template <typename T>
il void read(T &x)
{
	x=0;int f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	x*=f;
}
void Dijkstra(int s)
{
	memset(dis,0x3f,sizeof(dis));
	dis[s]=0,q.push(mp(0,s));
	while(!q.empty())
	{
		int u=q.top().se;q.pop();
		if(vis[u]) continue;
		vis[u]=1;
		for(auto [v,w]:g[u])
			if(dis[v]>dis[u]+w)
			{
				dis[v]=dis[u]+w;
				if(!vis[v]) q.push(mp(dis[v],v));
			}
	}
}
int main()
{
	read(n),read(m),read(s);
	for(int i=1,u,v,w;i<=m;i++)
	{
		read(u),read(v),read(w);
		g[u].pb((Edge){v,w});
	}
	Dijkstra(s);
	for(int i=1;i<=n;i++) printf("%d ",dis[i]);
	return 0;
}

标签:GCC,ch,int,pragma,optimize,模板,define
From: https://www.cnblogs.com/lunjiahao/p/18560792/template

相关文章

  • 实验4 类的组合、继承、模板类、标准库
    1.实验任务1task1_1.cpp1#include<iostream>2usingnamespacestd;34classA{5public:6A(intx0,inty0);7voiddisplay()const;8private:9intx,y;10};11A::A(intx0,inty0):x{x0},y{y0}{}12voidA::display()con......
  • 【模板】可并堆 之 左偏树
    **P3377【模版】左偏树/可并堆**#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+10;intn,m;structHeap{ intls,rs; intdist,val,fa;}tr[N];intfifa(intx){ returntr[x].fa==x?x:tr[x].fa=fifa(tr[x].fa);}intmerge(int......
  • 【模板】朱刘算法
    【模板】朱刘算法#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+2;introot,n,m;structEdge{ intu,v,w;}e[N];intid[N],vis[N],pre[N],incost[N];voidzhuliu(){ inttn=0; intres=0; while(1) { tn=0; for(inti=1;i<=n;i++){......
  • 模板
    数据结构线段树2voidbuild(intp,intl,intr){ l(p)=l,r(p)=r; if(l==r)return; intmid=l+r>>1; build(ls(p)=p<<1,l,mid),build(rs(p)=p<<1|1,mid+1,r);}voidpushdown(intp){ sum(ls(p))=(sum(ls(p))*mul(p)+add(p......
  • vxe-grid 自定义插槽模板
    在vxe-table中使用vxe-grid渲染表格,当配置式不能满足需求时,。需要使用自定义插槽模板来自定义业务需求,实现更灵活的功能。vxe-grid支持多种自定义方式,可以使用插槽模板,也可以使用插槽来自定义模板。自定义单元格模板<template><div><vxe-gridv-bind="gridOptions......
  • 【wx小程序开发】模板调用 data 里的函数
    微信小程序glass-easel组件框架新增特性中支持在模板中调用data里的函数。如果data中的某个字段是函数,在模板里可以直接调用它:Component({data:{getDataField(){return'someValue'},},})<view>{{getDataField()}}</view>尽管这样做有时会......
  • 贵阳业财模板
    一、jenkins流水线模板:pipeline{agentanyenvironment{CLEANED_BRANCH="${params.BRANCH}".replace("/","-")BUILD_TAG="${CLEANED_BRANCH}-${newDate().format('yyyyMMddHHmmssSSS')}-${env.BUILD_......
  • 实验4 类的组合、继承、模板类、标准库
    task2:代码:1#include<iostream>2#include<vector>3#include<string>4#include<algorithm>5#include<numeric>6#include<iomanip>78usingstd::vector;9usingstd::string;10usingstd::c......
  • 【模板】状压DP
    **[POI2004]PRZ****考察内容:二进制子集遍历,DP转移**#include<bits/stdc++.h>usingnamespacestd;intn,W;structdata1{ intt,w;}a[20];intdp[(1<<20)],tt[(1<<20)],ww[(1<<20)];intmain(){ scanf("%d%d",&W,&n); for(......
  • 实验4 类的组合、继承、模板类、标准库
    实验2GradeCalc.hpp#include<iostream>#include<vector>#include<string>#include<algorithm>#include<numeric>#include<iomanip>usingstd::vector;usingstd::string;usingstd::cin;usingstd::cout;usingstd::en......