NOIP板子

1.最短路

  • 单源最短路

        (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]); }

 2.快速幂

inline ll mul(ll a,ll b,ll mod){
    ll ans=1;
    while(b){
        if(b&1) ans=ans*a%p;
        a=a*a%p;    b>>=1;
    }
    return ans%mod;
}

 3.拓展欧几里得

void ex_gcd(int a, int b, int &x, int &y) {
    if(b) ex_gcd(b, a % b, y, x), y -= (a / b) * x;
    else x = 1, y = 0;
}

猜你喜欢

转载自www.cnblogs.com/phemiku/p/11390825.html