算法基础课

第一章 基础算法

快速排序

快速排序

分析:
代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define pb push_back
#define pu push
#define fi first
#define se second
typedef pair<int,int> PII;
const int N = 2e5 + 10;
int w[N];

void quick_sort(int l, int r) {
	if(l >= r) return;
	int i = l - 1, j = r + 1;
	int p = w[l + r >> 1];
	while(i < j) {
		do i++; while(w[i] < p);
		do j--; while(w[j] > p);
	    if(i < j) swap(w[i], w[j]);
	}
	quick_sort(l, j), quick_sort(j + 1, r);
}
int main() {
	int n; cin >> n;
	for(int i = 0; i < n; i++) cin >> w[i];
	quick_sort(0, n - 1);
	for(int i = 0; i < n; i++) cout << w[i] << ' ';
	cout << endl;
	return 0;
}

第k个数

分析:
代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define pb push_back
#define pu push
#define fi first
#define se second
typedef pair<int,int> PII;
const int N = 2e5 + 10;
int w[N];

void quick_sort(int l, int r) {
	if(l >= r) return;
	int i = l - 1, j = r + 1;
	int p = w[l + r >> 1];
	while(i < j) {
		do i++; while(w[i] < p);
		do j--; while(w[j] > p);
		if(i < j) swap(w[i], w[j]);
	}

	quick_sort(l, j), quick_sort(j + 1, r);
}
int main() {
	int n, k; cin >> n >> k;
	for(int i = 0; i < n; i++) cin >> w[i];
	quick_sort(0, n - 1);
	cout << w[k - 1] << endl;
	return 0;
}

第二章 数据结构

第三章 搜索与图论

DFS

BFS

树与图的深度优先遍历

树与图的广度优先遍历

拓扑排序

Dijstra

Dijkstra求最短路(朴素版)

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 510;
int g[N][N];
int dist[N];
bool st[N];
int n, m;

int dijstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    for(int i = 0; i < n; i++) {
        int t = -1;
        for(int j = 1; j <= n; j++) {
            if(!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        }
        st[t] = true;
        for(int j = 1; j <= n; j++) 
            dist[j] = min(dist[j], dist[t] + g[t][j]);
    }
    if(dist[n] == 0x3f3f3f3f) return -1;
    else return dist[n];
}
int main() {
    cin >> n >> m;
    memset(g, 0x3f, sizeof g);
    for(int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c;
        g[a][b] = min(g[a][b], c);
    }
    cout << dijstra() << endl;
    return 0;
}

Dijkstra求最短路(堆优化版)

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> PII;
const int N = 2e5 + 10;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
bool st[N];
int n, m;

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

int dijstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});
    while(heap.size()) {
        auto t = heap.top();
        heap.pop();
        int b = t.first, a = t.second;
        if(st[a]) continue;
        st[a] = true;
        for(int i = h[a]; ~i; i = ne[i]) {
            int j = e[i];
            if(dist[j] > b + w[i]) {
                dist[j] = b + w[i];
                heap.push({dist[j], j});
            }
        }
    }
    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}
int main() {
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for(int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c;
        add(a, b, c);
    }
    cout << dijstra() << endl;
    return 0;
}

bellman-ford

有边数限制的最短路

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 510, M = 10010;
struct node {
    int a, b, w;
}edge[M];
int dist[N], backup[N];
int n, m, k;

int bellman_ford() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    for(int i = 0; i < k; i++) {
        memcpy(backup, dist, sizeof backup);
        for(int j = 0; j < m; j++) {
            dist[edge[j].b] = min(dist[edge[j].b], backup[edge[j].a] + edge[j].w);
        }
    }
    if(dist[n] > 0x3f3f3f3f / 2) return 0x3f3f3f3f;
    else return dist[n];
}
int main() {
    cin >> n >> m >> k;
    for(int i = 0; i < m; i++) {
        int a, b, w; cin >> a >> b >> w;
        edge[i] = {a, b, w};
    }
    int t = bellman_ford();
    if(t == 0x3f3f3f3f) cout << "impossible" << endl;
    else cout << t << endl;
    return 0;
}

spfa

spfa求最短路

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int h[N], e[N], ne[N], w[N], idx;
int n, m;
int dist[N];
bool st[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int spfa() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    queue<int>q;
    q.push(1);
    st[1] = true;
    while(q.size()) {
        int t = q.front();
        q.pop();
        st[t] = false;
        for(int i = h[t]; i != -1; i = ne[i]) {
            int j = e[i];
            if(dist[j] > dist[t] + w[i]) {
                dist[j] = dist[t] + w[i];
                if(!st[j]) {
                    st[j] = 1;
                    q.push(j);
                }
            }
        }
    }
    if(dist[n] > 0x3f3f3f3f / 2) return 0x3f3f3f3f;
    return dist[n];
}
int main() {
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for(int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c;
        add(a, b, c);
    }
    int t = spfa();
    if(t == 0x3f3f3f3f) cout << "impossible" << endl;
    else cout << t << endl;
    return 0;
}

spfa判断负环

代码:

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N = 10010;
int h[N], e[N], ne[N], w[N], idx;
bool st[N];
int cnt[N];
int dist[N];
int n, m;

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa() {
    queue<int>q;
    for(int i = 1; i <= n; i++) {
        st[i] = 1;
        q.push(i);
    }
    while(q.size()) {
        int t = q.front();
        st[t] = false;
        q.pop();
        for(int i = h[t]; i != -1; i = ne[i]) {
            int j = e[i];
            if(dist[j] > dist[t] + w[i]) {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;
                if(cnt[j] >= n) return true;
                if(!st[j]) {
                    st[j] = 1;
                    q.push(j);
                }
            }
        }
    }
    return false;
}
int main() {
    cin >> n >> m;
    memset(h, -1 ,sizeof h);
    for(int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c;
        add(a, b, c);
    }
    if(spfa()) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

Floyd

Floyd求最短路

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210;
int g[N][N];

int main() {
    int n, m, Q;
    cin >> n >> m >> Q;
    memset(g, 0x3f, sizeof g);
    for(int i = 1; i <= n; i++) g[i][i] = 0;
    while(m--) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = min(g[a][b], c);
    }
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    while(Q--) {
        int x, y; cin >> x >> y;
        if(g[x][y] > 0x3f3f3f3f / 2) cout << "impossible" << endl;
        else cout << g[x][y] << endl;
    }
    return 0;
}

Prim

Prim算法求最小生成树

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 510;
int g[N][N];
int dist[N];
bool st[N];
int n, m;

int prim() {
    memset(dist, 0x3f, sizeof dist);
    int ans = 0;
    for(int i = 0; i < n; i++) {
        int t = -1;
        for(int j = 1; j <= n; j++) {
            if(!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        }
        st[t] = true;
        if(i && dist[t] == 0x3f3f3f3f) return 0x3f3f3f3f;
        if(i) ans += dist[t];
        for(int j = 1; j <= n; j++) 
            dist[j] = min(dist[j], g[t][j]);
    }
    return ans;
}
int main() {
    cin >> n >> m;
    memset(g, 0x3f, sizeof g);
    for(int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c;
        g[a][b] = g[b][a] = min(g[a][b], c);
    }
    int t = prim();
    if(t == 0x3f3f3f3f) puts("impossible");
    else cout << t << endl;
    return 0;
}

Kruskal

Kruskal算法求最小生成树

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2e5 + 10;
struct node {
    int a, b, w;
}edge[N];
int p[N];

bool cmp(node a, node b) {
    return a.w < b.w;
}

int find(int x) {
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main() {
    int n, m; cin >> n >> m;
    for(int i = 1; i <= n; i++) p[i] = i;
    for(int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        edge[i] = {a, b, c};
    }
    int ans = 0, cnt = 0;
    sort(edge, edge + m, cmp);
    
    for(int i = 0; i < m; i++) {
        int a = edge[i].a, b = edge[i].b, w = edge[i].w;
        a = find(a), b = find(b);
        if(a != b) {
            ans += w;
            cnt++;
            p[a] = b;
        }
    }
    if(cnt < n - 1) cout << "impossible" << endl;
    else cout << ans << endl;
    return 0;
}

染色法判定二分图

染色法判定二分图

DFS代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
int h[M], e[M], ne[M], idx;
int color[N];

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool dfs(int u, int c) {
    color[u] = c;
    for(int i = h[u]; i != -1; i = ne[i]) {
        int j = e[i];
        if(!color[j]) {
            if(!dfs(j, 3 - c)) return false;
        }else if(color[j] == c) return false;
    }
    return true;
}
int main() {
    int n, m; cin >> n >> m;
    memset(h, -1, sizeof h);
    for(int i = 1; i <= m; i++) {
        int a, b; cin >> a >> b;
        add(a, b);
        add(b, a);
    }
    bool flag = true;
    for(int i = 1; i <= n; i++) {
        if(!color[i]) {
            if(!dfs(i, 1)) {
                flag = false;
                break;
            }
        }
    }
    if(flag) puts("Yes");
    else puts("No");
    
    return 0;
}

匈牙利算法

二分图的最大匹配

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 510, M = 1e5 + 10;
int h[M], e[M], ne[M], idx;
int a[N], b[N], match[N];
bool st[N];

int n1, n2, m;

void add(int x, int y) {
    e[idx] = y, ne[idx] = h[x], h[x] = idx++;
}
bool find(int u) {
    for(int i = h[u]; i != -1; i = ne[i]) {
        int j = e[i];
        if(!st[j]) {
            st[j] = true;
            if(match[j] == 0 || find(match[j])) {
                match[j] = u;
                return true;
            }
        }
    }
    return false;;
}
int main() {
    cin >> n1 >> n2 >> m;
    memset(h, -1, sizeof h);
    while(m--) {
        int x, y; cin >> x >> y;
        add(x, y);
    }
    int cnt = 0;
    for(int i = 1; i <= n1; i++) {
        memset(st, false, sizeof st);
        if(find(i)) cnt++;
    }
    cout << cnt << endl;
    return 0;
}
posted @ 2022-07-07 10:53  飘向远方丶  阅读(29)  评论(0编辑  收藏  举报