收录各种算法的模版
线段树
C++#include<bits/stdc++.h>
using namespace std;
int f[2000000],t,n,m,a[500000];
inline void buildtree(int k,int l,int r){
//k为标号,l是区间的左端点,r是区间的右端点
if(l==r){
f[k]=a[l];
return;
}
int m=(l+r)>>1;
buildtree(k+k,l,m);//左子树编号是k+k
buildtree(k+k+1,m+1,r);//右子树编号是k+k+1
f[k]=f[k+k]+f[k+k+1];//左右子树和
}
inline void add(int k,int l,int r,int x,int y){
//下标为k的区间,区间是l-r,里面下标为x的值加上y
f[k]+=y;
if(l==r) return;
int m=(l+r)>>1;
if(x<=m){
add(k+k,l,m,x,y);
}else{
add(k+k+1,m+1,r,x,y);
}
}
int calc(int k,int l,int r,int s,int t){
//下标是k的结点,区间是l-r,求s-t的和
if(l==s&&r==t){
return f[k];
}
int m=(l+r)>>1;
if(t<=m){
return calc(k+k,l,m,s,t);
}else{
if(s>m){
return calc(k+k+1,m+1,r,s,t);
}else{
return calc(k+k,l,m,s,m)+calc(k+k+1,m+1,r,m+1,t);
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
buildtree(1,1,n);
for(int i=1;i<=m;i++){
int op,x,y;
cin>>op>>x>>y;
if(op==1){
add(1,1,n,x,y);
}else{
cout<<calc(1,1,n,x,y)<<endl;
}
for(int i=1;i<=n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}
floyd
C++#include<bits/stdc++.h>
using namespace std;
int floyd[1005][1005],n,m;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) floyd[i][j]=0;
else floyd[i][j]=1e9;
}
}
while(m--){
int a,b,c;
cin>>a>>b>>c;
floyd[a][b]=min(floyd[a][b],c);
floyd[b][a]=min(floyd[b][a],c);
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
floyd[i][j]=min(floyd[i][j],floyd[i][k]+floyd[k][j]);
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<floyd[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
dijkstra
C++#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1.5e5 + 10;
int n, m, dis[N];
bool vis[N];
int h[N], ne[N], w[N], e[N], idx;
//链表存储边的关系
void add(int x,int y,int z){
w[++idx] = z, e[idx] = y, ne[idx] = h[x], h[x] = idx;
}
int dijkstra(){
//优先队列存储距离和点,按离源点距离排序,越小越在前面
//源点 x = 1, dis[1] = 0
priority_queue<PII, vector<PII>, greater<PII> > p_q;
p_q.push({0, 1} );
dis[1] = 0 ;
while(!p_q.empty() ){
auto t = p_q.top();
p_q.pop();
//如果这个点已经被选取确定遍历过它的边,则跳过
//否则选取这个点并设置为 true
int d = t.first, u = t.second;
if(vis[u]) continue;
vis[u] = true;
//利用u的边去更新和u有联系的点
for(int i = h[u]; ~i; i = ne[i] ){
int j = e[i];
if(dis[j] > d + w[i]){
dis[j] = d + w[i];
p_q.push({dis[j], j} );
}
}
}
if(dis[n] == 0x3f3f3f3f ) return -1;
return dis[n];
}
int main(){
memset(h, -1, sizeof(h) );
memset(dis, 0x3f, sizeof(dis) );
cin >> n >> m;
for(int i = 0; i < m; i ++ ){
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
}
cout<<dijkstra()<<endl;
}
spfa
C++#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, inf = 0x3f3f3f3f;
int h[N], e[N], ne[N], w[N], idx = 0;
int n, m, dis[N];
bool st[N];
void add(int x, int y, int z){
e[idx] = y, ne[idx] = h[x], w[idx] = z, h[x] = idx ++;
}
void spfa(){
queue<int> q;
q.push(1);
dis[1] = 0;
st[1] = true;
/* 判断整个图是否有负环,先全部入队列
for(int i = 1; i <= n; i ++ ){
q.push(i);
st[i] = true;
}
*/
while(!q.empty() ){
//先初始化放入初始源点
int u = q.front();
q.pop();
st[u] = false;
//将此点优化过的点再次放入队列,重复这个过程直至队列为空即可
for(int i = h[u]; ~ i; i = ne[i] ){
int j = e[i];
if(dis[j] > dis[u] + w[i] ){
dis[j] = dis[u] + w[i];
// cnt[j] = cnt[u] + 1;
// if (cnt[j] >= n) puts("存在负环")
if(!st[j]){
q.push(j);
st[j] = true;
}
}
}
}
}
int main(){
memset(h, -1, sizeof(h) );
memset(dis, 0x3f, sizeof(dis) );
cin >> n >> m;
for(int i =0; i < m; i ++ ){
int x, y, z;
cin >> x >> y >>z;
add(x, y, z);
}
spfa();
if(dis[n] == inf) puts("impossible");
else cout << dis[n] << endl;
}