Algo-Trian
This commit is contained in:
48
Algorithm/Graph/BFS/P1443 马的遍历.cpp
Normal file
48
Algorithm/Graph/BFS/P1443 马的遍历.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <tuple>
|
||||
#include <climits>
|
||||
using namespace std;
|
||||
|
||||
const int INF = INT_MAX >> 1;
|
||||
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
|
||||
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
|
||||
|
||||
bool check(int bx, int by, int x, int y) {
|
||||
return (x >= 0 && x < bx && y >= 0 && y < by);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, m, x, y;
|
||||
cin >> n >> m >> x >> y;
|
||||
vector<vector<int>> mp(n, vector<int>(m, INF));
|
||||
|
||||
x--; y--; // ת<><D7AA><EFBFBD><EFBFBD> 0-based
|
||||
queue<tuple<int, int, int>> q;
|
||||
q.push({x, y, 0});
|
||||
mp[x][y] = 0;
|
||||
|
||||
while (!q.empty()) {
|
||||
auto [qx, qy, path] = q.front();
|
||||
q.pop();
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int nx = qx + dx[i];
|
||||
int ny = qy + dy[i];
|
||||
if (check(n, m, nx, ny) && mp[nx][ny] == INF) {
|
||||
mp[nx][ny] = path + 1;
|
||||
q.push({nx, ny, path + 1});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &row : mp) {
|
||||
for (auto v : row) {
|
||||
cout << (v == INF ? -1 : v) << " ";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
48
Algorithm/Graph/Bellman-Ford有负边的最短路径.cpp
Normal file
48
Algorithm/Graph/Bellman-Ford有负边的最短路径.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
#include <climits>
|
||||
using namespace std;
|
||||
const int INF = INT_MAX >> 1;
|
||||
int main(){
|
||||
int n, edge_num, start;
|
||||
cin >> n >> edge_num >> start;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>Ҫadj, <20><>Ҫdp <20><> edges
|
||||
vector<tuple<int, int , int>> edges(edge_num);// p1, p2, w
|
||||
vector<int> dp(n, INF);
|
||||
dp[start] = 0;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
for(int i = 0; i < edge_num; i++){
|
||||
int a, b, w;
|
||||
cin >> a >> b >> w;
|
||||
edges[i] = {a, b, w};
|
||||
}
|
||||
|
||||
//<2F>Ƚ<EFBFBD><C8BD><EFBFBD>n - 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for(int i = 0; i < n - 1; i++){
|
||||
for(auto [p1, p2, w] : edges){
|
||||
if(dp[p2] > dp[p1] + w){
|
||||
dp[p2] = dp[p1] + w;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//<2F>Ƿ<EFBFBD><C7B7>и<EFBFBD><D0B8><EFBFBD>
|
||||
bool negcc = false;
|
||||
for(auto [p1, p2, w] : edges){
|
||||
if(dp[p2] > dp[p1] + w){
|
||||
negcc = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(negcc) cout << "Negative Circle Detected" << endl;
|
||||
else{
|
||||
for(auto x : dp){
|
||||
cout << x << " ";
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
39
Algorithm/Graph/DFS/P1036 [NOIP 2002 普及组] 选数.cpp
Normal file
39
Algorithm/Graph/DFS/P1036 [NOIP 2002 普及组] 选数.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
int js = 0;
|
||||
vector<int> arr;
|
||||
|
||||
bool checkprime(int x) {
|
||||
if (x < 2) return false;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void dfs(int idx, int k, int sum) {
|
||||
if (k == 0) { // <20>Ѿ<EFBFBD>ѡ<EFBFBD><D1A1>k<EFBFBD><6B>
|
||||
if (checkprime(sum)) js++;
|
||||
return;
|
||||
}
|
||||
if (idx >= arr.size()) return;
|
||||
|
||||
// ѡ<><D1A1><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>
|
||||
dfs(idx + 1, k - 1, sum + arr[idx]);
|
||||
// <20><>ѡ<EFBFBD><D1A1>ǰ<EFBFBD><C7B0>
|
||||
dfs(idx + 1, k, sum);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
arr.resize(n);
|
||||
for (int i = 0; i < n; i++) cin >> arr[i];
|
||||
|
||||
dfs(0, k, 0);
|
||||
cout << js << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
46
Algorithm/Graph/Dijkstra最短路径.cpp
Normal file
46
Algorithm/Graph/Dijkstra最短路径.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int INF = INT_MAX >> 1;
|
||||
int main(){
|
||||
int n, edge_num, start;
|
||||
cin >> n >> edge_num >> start;
|
||||
vector<vector<pair<int, int>>> adj(n);
|
||||
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> dp;
|
||||
vector<bool> used(n, false);
|
||||
vector<int> dist(n, INF);
|
||||
|
||||
for(int i = 0; i < edge_num; i++){
|
||||
int a, b, w;
|
||||
cin >> a >> b >> w;
|
||||
adj[a].push_back({b, w});
|
||||
adj[b].push_back({a, w});
|
||||
}
|
||||
dp.push({0, start});
|
||||
dist[start] = 0;
|
||||
|
||||
while(!dp.empty()){
|
||||
//ȷ<><C8B7>ѡ
|
||||
auto[dists, v] = dp.top();
|
||||
dp.pop();
|
||||
if(used[v]) continue;
|
||||
used[v] = true;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>
|
||||
for(auto [u, d] : adj[v]){
|
||||
if(!used[u] && dist[v] + d < dist[u]){//<2F><>С
|
||||
dist[u] = dist[v] + d;
|
||||
dp.push({dist[u], u});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto x : dist){
|
||||
if(x != INF) cout << x << " ";
|
||||
else cout << "INF" << " ";
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
37
Algorithm/Graph/Floyd两点之间最短路径.cpp
Normal file
37
Algorithm/Graph/Floyd两点之间最短路径.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
using namespace std;
|
||||
const int INF = 100001;//<2F><><EFBFBD><EFBFBD>
|
||||
int main(){
|
||||
//dp<64>ķֽ<D6BD><D7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD>ӽṹ<D3BD><E1B9B9><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>)
|
||||
int n, edge_num, start, end;
|
||||
cin >> n >> edge_num >> start >> end;
|
||||
//<2F><><EFBFBD>ش洢
|
||||
//vector<tuple<int, int, int>> edge(edge_num);
|
||||
vector<vector<int>> dp(n, vector<int>(n, INF));
|
||||
|
||||
for(int i = 0; i < edge_num; i++){
|
||||
int a, b, w;
|
||||
cin >> a >> b >> w;
|
||||
//edge[i] = {a, b, w};
|
||||
dp[a][b] = w;
|
||||
}
|
||||
|
||||
//dp, һ<><D2BB><EFBFBD><EFBFBD>kд<6B><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for(int k = 0; k < n; k++){
|
||||
for(int i = 0; i < n; i++){
|
||||
for(int j = 0; j < n; j++){
|
||||
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int k = 0; k < n; k++){
|
||||
if(dp[k][k] < 0){
|
||||
cout << "Negative Cycle Detected!" << endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
cout << dp[start][end] << endl;
|
||||
return 0;
|
||||
}
|
||||
72
Algorithm/Graph/Kruskal最小生成树.cpp
Normal file
72
Algorithm/Graph/Kruskal最小生成树.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
vector<int> parent;
|
||||
struct edge{
|
||||
int from;
|
||||
int to;
|
||||
int weight;
|
||||
|
||||
edge() : from(-1), to(-1), weight(-1){}
|
||||
edge(int from_, int to_, int weight_): from(from_), to(to_), weight(weight_){}
|
||||
};
|
||||
struct cmp{
|
||||
bool operator()(const edge& a, const edge& b){
|
||||
return a.weight < b.weight;
|
||||
}
|
||||
};
|
||||
|
||||
int find(int x){
|
||||
if(parent[x] != x) parent[x] = find(parent[x]);
|
||||
return parent[x];
|
||||
}
|
||||
void merge(int x, int y){
|
||||
int rootX = find(x);
|
||||
int rootY = find(y);
|
||||
if(rootX != rootY) parent[rootY] = rootX;
|
||||
return;
|
||||
}
|
||||
|
||||
int main(){
|
||||
//<2F><>adj<64>ķ<EFBFBD>ʽ
|
||||
int n;
|
||||
cin >> n;
|
||||
//vector<vector<pair<int,int>>> adj(n, vector<pair<int,int>>());//<2F><><EFBFBD><EFBFBD>û<EFBFBD><C3BB>
|
||||
parent.resize(n);
|
||||
for(int i = 0; i < n; i++){
|
||||
parent[i] = i;
|
||||
}
|
||||
|
||||
vector<pair<int, int>> result(n, {-1, -1});
|
||||
int path = 0;
|
||||
int edge_num;
|
||||
cin >> edge_num;
|
||||
vector<edge> arr(edge_num);
|
||||
for(int i = 0; i < edge_num; i++){
|
||||
int a, b, w;
|
||||
cin >> a >> b >> w;
|
||||
arr[i] = edge(a, b, w);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
if(edge_num < n - 1) {
|
||||
cout << "Not Valid Full Graph" << endl;
|
||||
return ;
|
||||
}
|
||||
|
||||
sort(arr.begin(), arr.end(), cmp());
|
||||
for(auto x : arr){
|
||||
if(find(x.from) == find(x.to)){
|
||||
continue;
|
||||
}
|
||||
merge(x.from, x.to);
|
||||
result.push_back({x.from, x.to});
|
||||
path += x.weight;
|
||||
}
|
||||
cout << path << endl;
|
||||
for(auto x : result){
|
||||
if(x.first != -1 && x.second != -1) cout << "{" << x.first << ", " << x.second << "}" << " ";
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
9
Algorithm/Graph/P4779 【模板】单源最短路径(标准版).cpp
Normal file
9
Algorithm/Graph/P4779 【模板】单源最短路径(标准版).cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int v, e, start;
|
||||
cin >> v >> e >> start;
|
||||
vector<int> map(v, 0);
|
||||
return 0;
|
||||
}
|
||||
51
Algorithm/Graph/P5318 【深基18.例3】查找文献.cpp
Normal file
51
Algorithm/Graph/P5318 【深基18.例3】查找文献.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
using namespace std;
|
||||
vector<vector<int>> adj;
|
||||
vector<bool> used;
|
||||
//
|
||||
void dfs(int idx){
|
||||
if(used[idx]) return;
|
||||
used[idx] = true; // <20><><EFBFBD><EFBFBD><EFBFBD>ѷ<EFBFBD><D1B7><EFBFBD>
|
||||
cout << idx + 1 << " ";
|
||||
|
||||
for(auto x : adj[idx]){
|
||||
dfs(x);
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int v, e;
|
||||
cin >> v >> e;
|
||||
adj.resize(v);
|
||||
used.resize(v, false);
|
||||
for(int i = 0; i < e; i++){
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
adj[a - 1].push_back(b - 1);
|
||||
}
|
||||
for(int i = 0; i < v; i++){
|
||||
sort(adj[i].begin(), adj[i].end());
|
||||
}
|
||||
dfs(0);
|
||||
fill(used.begin(), used.end(), false);
|
||||
|
||||
cout << endl;
|
||||
//bfs
|
||||
deque<int> q;
|
||||
q.push_back(0);
|
||||
used[0] = true;
|
||||
while(!q.empty()){
|
||||
int top = q.front(); q.pop_front(); // ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> front()
|
||||
cout << top + 1 << " ";
|
||||
for(auto x : adj[top]){
|
||||
if(!used[x]){
|
||||
used[x] = true; // <20><><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>
|
||||
q.push_back(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user