东方 -图论

1.图的存储和遍历

2052. 图的dfs遍历

STL局部变量版本

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void dfs(int v, vector<bool>& visited, const vector<vector<int>>& adj) {
    visited[v] = true;
    cout << v << " ";

    for (int u : adj[v]) {
        if (!visited[u]) {
            dfs(u, visited, adj);
        }
    }
}

int main() {
    int n, e;
    cin >> n >> e;

    vector<vector<int>> adj(n + 1);
    vector<bool> visited(n + 1, false);

    for (int i = 0; i < e; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u); // 因为是无向图,所以需要在两个方向上添加边
    }

    // 将每个节点的邻接列表排序,以确保优先访问编号较小的节点
    for (int i = 1; i <= n; ++i) {
        sort(adj[i].begin(), adj[i].end());
    }

    // 从节点 1 开始 DFS
    dfs(1, visited, adj);

    return 0;
}

STL全局变量版本

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int>> adj;
vector<bool> visited;

void dfs(int v) {
    visited[v] = true;
    cout << v << " ";

    for (int u : adj[v]) {
        if (!visited[u]) {
            dfs(u);
        }
    }
}

int main() {
    int n, e;
    cin >> n >> e;

    adj.resize(n + 1);
    visited.resize(n + 1, false);

    for (int i = 0; i < e; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u); // 由于是无向图,添加两个方向的边
    }

    // 对每个节点的邻接列表排序
    for (int i = 1; i <= n; ++i) {
        sort(adj[i].begin(), adj[i].end());
    }

    // 从节点 1 开始 DFS
    dfs(1);

    return 0;
}

2053. 图的bfs遍历

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

void bfs(int start, const vector<vector<int>>& adj) {
    vector<bool> visited(adj.size(), false);
    queue<int> q;
    visited[start] = true;
    q.push(start);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        cout << v << " ";

        for (int u : adj[v]) {
            if (!visited[u]) {
                visited[u] = true;
                q.push(u);
            }
        }
    }
}

int main() {
    int n, e;
    cin >> n >> e;

    vector<vector<int>> adj(n + 1);

    for (int i = 0; i < e; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for (int i = 1; i <= n; ++i) {
        sort(adj[i].begin(), adj[i].end());
    }

    bfs(1, adj);

    return 0;
}

2053. 图的bfs遍历

STL局部变量版本

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

void bfs(int start, const vector<vector<int>>& adj) {
    vector<bool> visited(adj.size(), false);
    queue<int> q;
    visited[start] = true;
    q.push(start);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        cout << v << " ";

        for (int u : adj[v]) {
            if (!visited[u]) {
                visited[u] = true;
                q.push(u);
            }
        }
    }
}

int main() {
    int n, e;
    cin >> n >> e;

    vector<vector<int>> adj(n + 1);

    for (int i = 0; i < e; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for (int i = 1; i <= n; ++i) {
        sort(adj[i].begin(), adj[i].end());
    }

    bfs(1, adj);

    return 0;
}

STL全局变量版本

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

vector<vector<int>> adj;
vector<bool> visited;

void bfs(int start) {
    queue<int> q;
    visited[start] = true;
    q.push(start);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        cout << v << " ";

        for (int u : adj[v]) {
            if (!visited[u]) {
                visited[u] = true;
                q.push(u);
            }
        }
    }
}

int main() {
    int n, e;
    cin >> n >> e;

    adj.resize(n + 1);
    visited.resize(n + 1, false);

    for (int i = 0; i < e; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u); // 由于是无向图,添加两个方向的边
    }

    for (int i = 1; i <= n; ++i) {
        sort(adj[i].begin(), adj[i].end());
    }

    bfs(1);

    return 0;
}

2080. 邻接点

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n, e;
    cin >> n >> e;

    vector<vector<int>> adj(n + 1); // 邻接表

    for (int i = 0; i < e; ++i) {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y); // 将 y 添加到 x 的邻接列表中
    }

    // 对每个节点的邻接点列表进行排序
    for (int i = 1; i <= n; ++i) {
        sort(adj[i].begin(), adj[i].end());
    }

    // 遍历每个节点并输出其邻接点
    for (int i = 1; i <= n; ++i) {
        if (!adj[i].empty()) { // 仅当节点有邻接点时输出
            cout << i << endl;
            for (int j : adj[i]) {
                cout << j << " ";
            }
            cout << endl;
        }
    }

    return 0;
}

2.欧拉路

2055. 欧拉路

#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> m; // 邻接矩阵
vector<int> degree;    // 度数数组
vector<int> path;      // 路径数组

void dfs(int start) {
    for (int i = 1; i < m[start].size(); ++i) {
        if (m[start][i]) {
            m[start][i] = 0;
            m[i][start] = 0;
            dfs(i);
        }
    }
    path.push_back(start);
}

int main() {
    int n, e;
    cin >> n >> e;

    // 初始化全局变量
    m.resize(n + 1, vector<int>(n + 1, 0));
    degree.resize(n + 1, 0);

    int x, y;
    // 读取边信息并更新邻接矩阵和度数数组
    for (int i = 0; i < e; i++) {
        cin >> x >> y;
        m[x][y] = 1;
        m[y][x] = 1;
        degree[x]++;
        degree[y]++;
    }

    // 找到起始点,优先选择最大的奇数度点
    int start = 1;
    for (int i = n; i > 0; i--) {
        if (degree[i] % 2 == 1) {
            start = i;
            break;
        }
    }

    // 执行DFS搜索
    dfs(start);

    // 逆序输出路径构成欧拉路或回路
    for (int i = path.size() - 1; i >= 0; i--) {
        cout << path[i] << " ";
    }

    return 0;
}

3.最短路

2044. 城市之间的最短路

#include <iostream>
#include <vector>
#include <climits>
#include <queue>
using namespace std;

const int MAXN = 11;
const int INF = INT_MAX;
vector<pair<int, int>> adj(11); // 存储图的邻接列表(城市,距离)
// 起始点 终点 所有点 
int dijkstra(int start, int end, int n) {
	//存储的是起始点到每个节点的前缀和 
    vector<int> dist(n + 1, INF); // 创建一个大小为n+1的向量,存储从起点到每个节点的最短距离,初始值设为INF 
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; // 创建一个优先队列,用于存储和排序待处理的节点

    dist[start] = 0; // 将起点到自身的距离设置为0
    pq.push({0, start}); // 将起点及其距离加入优先队列

    while (!pq.empty()) { // 当队列不为空时执行
        int distance = pq.top().first; // 获取队列顶部节点的距离
        int current = pq.top().second; // 获取队列顶部的节点
        pq.pop(); // 弹出队列顶部元素

        if (distance > dist[current]) { // 如果当前距离大于已知的最短距离,则跳过
            continue;
        }

        for (auto edge : adj[current]) { // 遍历当前节点的所有邻接节点
            int next = edge.first; // 获取邻接节点
            int nextDist = distance + edge.second; // 计算到邻接节点的新距离

            if (nextDist < dist[next]) { // 如果新距离小于已知的最短距离
                dist[next] = nextDist; // 更新最短距离
                pq.push({nextDist, next}); // 将邻接节点加入优先队列
            }
        }
    }

    return dist[end]; // 返回从起点到终点的最短距离
}

int main() {
    int n, m; // 定义变量n和m,分别表示节点数和边数
    cin >> n >> m; // 从标准输入读取n和m的值

    int a, b, l; // 定义变量a, b和l,用于读取边的信息
    for (int i = 0; i < m; i++) { // 循环m次读取每条边的信息
        cin >> a >> b >> l; // 从标准输入读取边的起点a,终点b和长度l
        adj[a].push_back({b, l}); // 将边加入起点a的邻接列表
        adj[b].push_back({a, l}); // 将边加入终点b的邻接列表(因为是无向图)
    }

    int x, y; // 定义变量x和y,表示要查询的两个城市
    cin >> x >> y; // 从标准输入读取这两个城市的编号

    int shortestPath = dijkstra(x, y, n); // 调用dijkstra函数计算从x到y

    if (shortestPath != INF) {
        cout << shortestPath << endl;
    } else {
        cout << "No path" << endl;
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天秀信奥编程培训

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值