代码模版

· · 个人记录

搜索

广搜最短路模板

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define TT int T;cin>>T;while(T--)
const int N = 1e6 + 10, M = 1e9 + 7;
int n, m;
char a[101][101];
bool vis[101][101];
bool in(int x, int y) {
    return 1 <= x && x <= n && 1 <= y && y <= m;
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
struct node {
    int x, y, d; //(x,y) 走了d步
};
queue<node> q;//队列
signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    q.push(node{1, 1, 1});
    vis[1][1] = 1;
    while (q.size()) {
        node f = q.front(); //当前任务
        q.pop();
        //到终点
        if (f.x == n && f.y == m) {
            cout << f.d;
            return 0;
        }//做任务
        for (int i = 0; i < 4; i++) {
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if (in(tx, ty) && !vis[tx][ty] && a[tx][ty] != '#') {
                vis[tx][ty] = 1;
                q.push(node{tx, ty, f.d + 1});
            }
        }
    }
    return 0;
}

并查集

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10, M = 5e5 + 10;
int n, m;
int f[N];
int find(int x) {
    if (x == f[x]) return x;
    return f[x] = find(f[x]);
}
signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        f[i] = i;
    }
    while (m --) {
        int t, x, y;
        cin >> t >> x >> y;
        int X = find(x);
        int Y = find(y);
        if (t == 1) f[X] = Y;
        else if (t == 2) {
            if (X == Y) cout << "Y\n";
            else cout << "N\n";
        }
    }
    return 0;
}

深搜迷宫类模板

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, a[21][21];
bool vis[21][21];
int sx, sy, ex, ey;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool in(int x, int y) {
    return 1 <= x && x <= n && 1 <= y && y <= n;
}
void dfs(int x, int y) {
    if (x == ex && y == ey) {
        cout << "YES";
        exit(0);
    }
    for (int i = 0; i < 4; i++) {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if (in(tx, ty) && a[tx][ty] == 0 && vis[tx][ty] == 0) {
            vis[tx][ty] = 1;
            dfs(tx, ty);
        }
    }
}
signed main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }
    cin >> sx >> sy >> ex >> ey;
    dfs(sx, sy);
    cout << "NO";
    return 0;
}

图论

树状数组

#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(nullptr),cout.tie(nullptr);
#define memset(a) memset((a), (0), sizeof(a));
using namespace std;
const int N = 1e6+10, M = 1e2+10, MOD = 1e9+7;
int n, m;
int a[N], b[N];
int lowbit(int x) {
    return x & -x;
}
//单点修改 将x个增加y
void update(int x, int y) {
    while (x <= n) {
        b[x] += y;
        x += lowbit(x);
    }
}
//区间查询 1~x
int query(int x) {
    int s = 0;
    while (x) {
        s += b[x];
        x -= lowbit(x);
    }
    return s;
}
signed main() {

    IOS

    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        update(i, a[i]);
    }

    while (m--) {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1) {
            update(x, y);
        } else {
            cout << query(y) - query(x - 1) << "\n";
        }
    }
    return 0;
}

动态规划

背包

01背包

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
int t;
int m;
int w[110], v[110];
int dp[1100];
signed main() {
    cin >> t >> m;
    for (int i = 1; i <= m; i++) cin >> w[i] >> v[i];
    for (int i = 1; i <= m; i++) {
        for (int j = t; j >= w[i]; j--) {
            int nv = dp[j - w[i]] + v[i];
            dp[j] = max(dp[j], nv);
        }
    }
    cout << dp[t];
    return 0;
}

完全背包

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
int t;
int m;
int w[110], v[110];
int dp[1100];
signed main() {
    cin >> t >> m;
    for (int i = 1; i <= m; i ++) cin >> w[i] >> v[i];
    for (int i = 1; i <= m; i ++) {
        for (int j = w[i]; j <= t; j ++) {
            int nv = dp[j - w[i]] + v[i];
            dp[j] = max(dp[j], nv);
        }
    }
    cout << dp[t];
    return 0;
}