自用板子
Posted phemiku
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自用板子相关的知识,希望对你有一定的参考价值。
最短路
单源最短路(1~n)
//SPFA算法
#include<bits/stdc++.h> using namespace std; const int N= 15000; struct bian int x,y,d,next; ; int last[N],len,d[N]; int List[N],tail,head,v[N]; bian a[210000]; void ins(int x,int y,int d) len++; a[len].x=x;a[len].y=y;a[len].d=d; a[len].next=last[x]; last[x]=len; int main() int n,m,x,y,c,st,ed; scanf("%d%d",&n,&m); //点,边 for(int i=1;i<=m;i++) scanf("%d%d%d",&x,&y,&c); ins(x,y,c); ins(y,x,c); for(int i=1;i<=n;i++)d[i]=9999999999; st=1;ed=n; d[st]=0;v[st]=1; List[1]=st;head=1;tail=2; while(head!=tail) x=List[head]; for(int k=last[x];k;k=a[k].next) y=a[k].y; if(d[y]>d[x]+a[k].d) d[y]=d[x]+a[k].d; if(v[y]==0) v[y]==1; List[tail]=y; tail++; if(tail==n+1) tail=1; List[head]=0; head++; if(head==n+1)head=1; v[x]=0; printf("%d",d[n]);
//Dijsktra裸题 忘了题号了
#include<iostream> #include<cstring> using namespace std; #define INF 0x3f3f3f3f const int N = 150; int n,m,x,y,z,p; int a[N][N],dis[N]; bool vis[N]; void dij(int x, int y) for(int i = 1; i <= n; i++) dis[i] = a[1][i]; //单源最短路 vis[i] = false; vis[x] = 1; for(int i = 1; i <= n; i++) int minn = INF; for(int j = 1; j <= n; j++) if(!vis[j]&&dis[j]<minn) minn = dis[j]; p = j; vis[p] = 1; for(int j = 1; j <= n; j++) if(!vis[j]&&dis[p]+a[p][j]<dis[j]) dis[j] = dis[p] + a[p][j]; int main() while(cin >> n >> m) if(n == 0 && m == 0) break; memset(a,INF,sizeof(a)); while(m--) cin >> x >> y >> z; a[x][y] = a[y][x] = z; dij(1,n); cout << dis[n] << endl; return 0;
2.快速幂和快速乘
#include<bits/stdc++.h> using namespace std; #define int long long //这样就不用每次开long long了 const int N = 100050; inline int read() //快读 char c = getchar(); int x = 0, f = 1; while(c < ‘0‘ || c > ‘9‘) if(c == ‘-‘) f = -1; c = getchar(); while(c >= ‘0‘ && c <= ‘9‘) x = x * 10 + c - ‘0‘, c = getchar(); return x * f; inline int qpow(int a,int b,int mod) //快速幂 for(int c = 1; ;a = a * a % mod) if(b & 1)c = c * a % mod; if(!(b >>= 1))return c; inline int qmul(int a,int b,int mod) //快速乘 for(int c = 0; ;a = (a << 1) % mod) if(b & 1)c = (c + a) % mod; if(!(b >>= 1))return c; int n,m,a,b,q,c,d,r; signed t; signed main() t = read(); for(int i = 1;i <= t; i++) a = read();b = read();q = read(); c = read();d = read();r = read(); cout << qpow(a,b,q) << " "<< qmul(c,d,r) << endl ;
3.二分
int l = 1; int r = d; while (l <= r) int mid = (l + r) / 2; if (check(mid)) ans = mid; l = mid + 1; else r = mid - 1;
4.高精度
高精度减法
#include<bits/stdc++.h> using namespace std; const int N= 10500; int na[N],nb[N],nc[N]; bool bj; string a,b; int main() cin>>a>>b; if(a.size()<b.size()||(a.size()==b.size()&&a<b)) //我果然年少又无知…string可以直接比较大小的…(ASCII码 bj=1; swap(a,b); for(int i=a.size();i>=1;i--)na[i]=a[a.size()-i]-‘0‘; for(int i=b.size();i>=1;i--)nb[i]=b[b.size()-i]-‘0‘; int n=max(a.size(),b.size()); for(int i = 1; i <= n; i ++) if(na[i] < nb[i]) na[i + 1] --; na[i] += 10; nc[i] = na[i] - nb[i]; while(nc[n]==0)n--; if(bj)cout<<"-"; for(int i=n;i>0;i--)cout<<nc[i]; if(n<1)cout<<"0";
高精度乘法
#include<iostream> #include<cstring> #define maxn 100000+10 using namespace std; int na[maxn],nb[maxn],nc[maxn]; char a[maxn],b[maxn]; void mul() int i,j,lena,lenb; memset(na,0,sizeof(na)); memset(nb,0,sizeof(nb)); memset(nc,0,sizeof(nc)); lena=strlen(a); lenb=strlen(b); for(i=0;i<lena;i++) na[i]=a[lena-i-1]-‘0‘; for(i=0;i<lenb;i++) nb[i]=b[lenb-i-1]-‘0‘; for(i=0;i<lena;i++) for(j=0;j<lenb;j++) nc[i+j]+=na[i]*nb[j]; int max=lena+lenb; for(i=0;i<max;i++) nc[i+1]+=nc[i]/10,nc[i]%=10; while(!nc[--max]); max++; for(i=0;i<max;i++) a[i]=nc[max-i-1]+‘0‘; a[max]=‘\0‘; int main() while(cin>>a>>b) mul(); puts(a); return 0;
5.欧几里得(辗转相除)
ll gcd(ll a, ll b) return !b ? a : gcd(b, a%b);
6.左偏树
100.乱七八糟的东西
//重载运算符比较大小
struct node int len; bool operator <(const node &a)const //重载<操作符。可以对两个node使用<操作符进行比较 return len<a.len; ;
以上是关于自用板子的主要内容,如果未能解决你的问题,请参考以下文章