版本比较
些许板子
正文
增加 144 行删除 60 行
112345678984 行未改动9495969797989910010110237 行未改动140141142143143144145145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201146147148149198 行未改动全部展开348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491双端队列### 双端队列```cpp#include<bits/stdc++.h>using namespace std;//有个东西叫做dequestruct Dque{ int q[10000*2]; int head=10000;84 行未改动 //主函数内容不必多说了吧(^-^) return 0;}```并查集### 并查集```cpp#include<bits/stdc++.h>using namespace std;
37 行未改动 //...(0-O)你说这里应该不需要给出来的,对吧? return 0;}```线段树### 平衡树```cpp#include <bits/stdc++.h>using namespace std;
int a[100000];
struct node{ int l,r; long long v,lazy;}tr[100000];
void build(int l,int r,int p)//建树{ tr[p].l=l; tr[p].r=r; if(l==r) { tr[p].v=a[l]; return ; } int mid=(l+r)/2; build(l,mid,p*2);//左子树 build(mid+1,r,p*2+1);//右子树 tr[p].v=tr[p*2].v+tr[p*2+1].v; return ;}
int addtag(int p,int d)//添加懒标记{ tr[p].lazy+=d; tr[p].v=tr[p].lazy*(tr[p].r-tr[p].l+1);}
int pushdown(int p)//下放懒标记{ if(tr[p].lazy) { int mid=(tr[p].l+tr[p].r)/2; addtag(mid*2,tr[p].lazy);//右儿子 addtag(mid*2-1,tr[p].lazy);//儿子 tr[p].lazy=0; }}
int update(int l,int r,int p,int d){ if(l<=tr[p].l&&tr[p].r<=r) { addtag(mid*2,tr[p].lazy); }}
//啊欧,没写完,等一等吧
```平衡树```cpp#include<bits/stdc++.h>using namespace std;const int N=1e5+10;const int inf=0x3f3f3f3f;198 行未改动全部展开 return 0;}
```### dijkstra```cpp#include <bits/stdc++.h>
using namespace std;long long n, m, s;
struct Edge{ long long to,w; bool operator<(const Edge &s) const { return s.w<w; }};
bool S[10001]; vector<Edge> G[10001]; long long dis[10001];
long long dijkstra(long long start){
priority_queue<Edge> q; q.push({start, 0}); for(long long i=1;i<=n;i++) { dis[i]=INT_MAX; }
dis[start]=0; while (q.size()>0) { Edge a=q.top(); q.pop(); long long u=a.to; if(S[u]) { continue; } S[u]=1; for (int i=0;i<G[u].size();i++) { int v = G[u][i].to; if (S[v] == 0) { dis[v] = min(dis[v],G[u][i].w+a.w); q.push({v,dis[v]}); } } } return 0;}
int main(){ cin>>n>>m>>s; for(long long i=0;i<m;i++) { long long u,v,w; cin>>u>>v>>w; G[u].push_back({v,w}); } dijkstra(s); for(long long i=1;i<=n;i++) { cout<<dis[i]<<" "; } return 0;}```### Prim```cpp#include <bits/stdc++.h>
using namespace std;
int n,m;int q[5001][5001];bool visited[5001];
int MST(){ vector<int> dist(n+1,0x3f3f3f3f); dist[1]=0; int mst_sum=0;
for (int i=0;i<n;i++) { int u=-1; int min_dist=0x3f3f3f3f; for (int j=1;j<=n;j++) { if (!visited[j]&&dist[j]<min_dist) { min_dist=dist[j]; u=j; } } if(u==-1) { return -1; } visited[u]=1; mst_sum+=dist[u]; for (int v=1;v<=n;v++) { if (!visited[v]&&q[u][v]<dist[v]) { dist[v]=q[u][v]; } } } return mst_sum;}
int main(){ memset(q, 0x3f, sizeof(q)); cin>>n>>m; for(int i=0;i<m;i++) { int u,v,w; cin>>u>>v>>w; q[u][v]=min(q[u][v],w); q[v][u]=min(q[v][u],w); } int ans=MST(); if(ans==-1) { cout<<"orz"<<endl; } else { cout<<ans<<endl; } return 0;}```