各种模板

发布时间 2023-10-06 15:37:22作者: Yun_Mengxi

【模板】栈

#include <stack>
#include <string>
#include <iostream>
using namespace std;
int main() {
	cin.tie(0);
	int t, n;
	for (cin >> t; t; --t) {
		stack<unsigned long long int> s;
		for (cin >> n; n; --n) {
			string t;
			cin >> t;
			if (t == "push") {
				unsigned long long x;
				cin >> x;
				s.push(x);
			} else if (t == "pop") {
				if (s.empty()) {
					cout << "Empty\n";
				} else {
					s.pop();
				}
			} else if (t == "query") {
				if (s.empty()) {
					cout << "Anguei!\n";
				} else {
					cout << s.top() << '\n';
				}
			} else {
				cout << s.size() << '\n';
			}
		}
	}
	return 0;
}

题目:link

【模板】队列

#include<iostream>
using namespace std;
int n, x, m;
int queue[10005], head = 1, tail = 0;
int main() {
	cin >> n;
	while (n--) {
		cin >> x;
		switch (x) {
			case 1:
				cin >> m;
				queue[++tail] = m;
				break;
			case 2:
				if (head > tail) {
					cout << "ERR_CANNOT_POP" << '\n';
				} else {
					head++;
				}
				break;
			case 3:
				if (head > tail) {
					cout << "ERR_CANNOT_QUERY" << '\n';
				} else {
					cout << queue[head] << '\n';
				}
				break;
			case 4:
				cout << tail - head + 1 << '\n';
				break;
		}
	}
	return 0;
}

题目:link

【模板】Floyd 算法

#include <bits/stdc++.h>
using namespace std;

int n, m;
int u, v, w;
int l[105][105];

int main() {
    memset(l, 0x3f, sizeof(l));
    for (int i = 1; i <= 100; i++) {
        l[i][i] = 0;
    }
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> u >> v >> w;
        l[u][v] = w;
        l[v][u] = w;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i != j) {
                for (int k = 1; k <= n; k++) {
                    if (l[j][k] > l[i][j] + l[i][k]) {
                        l[j][k] = l[i][j] + l[i][k];
                    }
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << l[i][j] << " ";
        }
        cout << '\n';
    }
    return 0;
}

题目:link

【模板】传递闭包

#include <bits/stdc++.h>
using namespace std;

int n, m;
int u, v, w;
int l[105][105];

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> l[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i != j) {
                for (int k = 1; k <= n; k++) {
                    l[j][k] |= (l[j][i] & l[i][k]);
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << l[i][j] << " ";
        }
        cout << '\n';
    }
    return 0;
}