图论
dijkstra
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #define ll long long using namespace std; const int maxn=200005; inline ll read(){ ll x=0,k=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return k*x; } int fir[maxn],nex[maxn],to[maxn],w[maxn],ecnt; int dis[maxn],vis[maxn]; void addedge(int u,int v,int wi){ nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;w[ecnt]=wi; } struct node{ int d,id; bool operator < (const node&rhs) const{ return d>rhs.d; } }; void dij(int s){ memset(dis,127,sizeof dis); memset(vis,0,sizeof vis); priority_queue<node> q; dis[s]=0; q.push((node){0,s}); while(!q.empty()){ node u=q.top(); q.pop(); if(vis[u.id]) continue; vis[u.id]=1; for(int i=fir[u.id];i;i=nex[i]){ int v=to[i]; if(u.d+w[i]<dis[v]){ dis[v]=u.d+w[i]; q.push((node){dis[v],v}); } } } } int main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); int n,m,s; n=read();m=read();s=read(); for(int i=1;i<=m;i++){ int x,y,z; x=read();y=read();z=read(); addedge(x,y,z); } dij(s); for(int i=1;i<=n;i++) cout<<dis[i]<<" "; return 0; }
spfa判负环
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #define ll long long using namespace std; const int maxn=6005; inline ll read(){ ll x=0,k=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return k*x; } int fir[maxn],nex[maxn],to[maxn],w[maxn],ecnt; int dis[maxn],vis[maxn],cnt[maxn]; void addedge(int u,int v,int wi){ nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;w[ecnt]=wi; } int n,m; bool spfa(int s){ memset(dis,127,sizeof dis); memset(vis,0,sizeof vis); memset(cnt,0,sizeof cnt); queue<int> q; q.push(s); dis[s]=0;vis[s]=1; while(!q.empty()){ int u=q.front(); q.pop();vis[u]=0; if(cnt[u]>=n) return true; for(int i=fir[u];i;i=nex[i]){ if(dis[u]+w[i]<dis[to[i]]){ dis[to[i]]=dis[u]+w[i]; if(!vis[to[i]]){ q.push(to[i]); vis[to[i]]=1; cnt[to[i]]++; if(cnt[to[i]]>=n) return true; } } } } return false; } int main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); int t; t=read(); while(t--){ ecnt=0;memset(fir,0,sizeof fir); n=read();m=read(); for(int i=1;i<=m;i++){ int a,b,w; a=read();b=read();w=read(); addedge(a,b,w); if(w>=0) addedge(b,a,w); } if(spfa(1)) cout<<"YE5"<<endl; else cout<<"N0"<<endl; } return 0; }
最短路计数
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #define ll long long using namespace std; const int maxn=4000005; const int mod=100003; inline ll read(){ ll x=0,k=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return k*x; } int fir[maxn],nex[maxn],to[maxn],w[maxn],ecnt; int dis[maxn],vis[maxn],cnt[maxn]; void addedge(int u,int v,int wi){ nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;w[ecnt]=wi; } int n,m; void spfa(int s){ memset(dis,127,sizeof dis); memset(vis,0,sizeof vis); queue<int> q; q.push(s); dis[s]=0;vis[s]=1;cnt[1]=1; while(!q.empty()){ int u=q.front(); q.pop();vis[u]=0; for(int i=fir[u];i;i=nex[i]){ if(dis[u]+1<dis[to[i]]){ dis[to[i]]=dis[u]+1; cnt[to[i]]=cnt[u]; if(!vis[to[i]]){ q.push(to[i]); vis[to[i]]=1; } } else if(dis[u]+1==dis[to[i]]){ cnt[to[i]]=(cnt[to[i]]+cnt[u])%mod; } } } } int main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); n=read();m=read(); for(int i=1;i<=m;i++){ int a,b; a=read();b=read(); addedge(a,b,1); addedge(b,a,1); } spfa(1); for(int i=1;i<=n;i++){ cout<<cnt[i]<<endl; } return 0; }
kruskal
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #define ll long long using namespace std; const int maxn=200005; const int mod=100003; inline ll read(){ ll x=0,k=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return k*x; } struct node{ int x,y,w; }a[maxn]; bool cmp(node xx,node yy){ return xx.w<yy.w; } int fa[maxn]; int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } int n,m,ans; void kruskal(){ int cnt=0; sort(a+1,a+m+1,cmp); for(int i=1;i<=m;i++){ if(find(a[i].x)!=find(a[i].y)){ fa[find(a[i].x)]=find(a[i].y); ans+=a[i].w; cnt++; } if(cnt==n-1) break; } } int main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); n=read();m=read(); for(int i=1;i<=m;i++){ a[i].x=read();a[i].y=read();a[i].w=read(); } for(int i=1;i<=n;i++) fa[i]=i; kruskal(); cout<<ans; return 0; }
数学
gcd
int gcd(int a,int b){ return b?gcd(b,a%b):a; }
exgcd
int exgcd(int a,int b,int &d,int &x,int &y){ if(!b) {d=a;x=1;y=0;} else{exgcd(b,a%b,d,y,x);y-=x*(a/b)} }