#include <iostream> #include <vector> using namespace std; // 定义边结构体 struct Edge { int to; // 目标节点 int weight; // 权重 Edge(int t, int w) : to(t), weight(w) {} // 构造函数 }; // 定义邻接表 vector<Edge> adj[100]; // 添加无向边 void addEdge(int u, int v, int weight) { adj[u].push_back(Edge(v, weight)); adj[v].push_back(Edge(u, weight)); } int main() { int n, m; // n为节点数,m为边数 cin >> n >> m; for (int i = 0; i < m; i++) { int u, v, weight; cin >> u >> v >> weight; addEdge(u, v, weight); // 添加边 } // 遍历所有节点,输出其邻接表 for (int u = 1; u <= n; u++) { cout << "Node " << u << " is adjacent to: "; for (auto e : adj[u]) { cout << e.to << "(" << e.weight << ") "; } cout << endl; } return 0; }这段代码34、37、38行有错请修改
时间: 2024-01-09 16:04:30 浏览: 30
将34行改为 `adj[u-1].push_back(Edge(v-1, weight));`,因为数组下标是从0开始的,而输入的节点编号是从1开始的,需要做一定的转换。
将37、38行改为:
```
cout << "Node " << u << " is adjacent to: ";
for (auto e : adj[u-1]) {
cout << e.to + 1 << "(" << e.weight << ") "; // 输出时需要将节点编号再加1
}
cout << endl;
```
同样需要对节点编号做转换。
相关问题
#include <iostream> #include <vector> #include <queue> using namespace std; // 定义图的边结构体 struct Edge { int to; // 边的终点 int weight; // 边的权重 Edge(int t, int w) : to(t), weight(w) {} }; // 定义图的邻接表结构体 struct Graph { int n; // 节点数 vector<vector<Edge>> adj; // 邻接表 Graph(int n) : n(n), adj(n) {} // 添加一条边 void addEdge(int from, int to, int weight) { adj[from].push_back(Edge(to, weight)); } // 深度优先遍历 void dfs(int cur, vector<bool>& visited) { visited[cur] = true; cout << cur << " "; for (auto e : adj[cur]) { if (!visited[e.to]) { dfs(e.to, visited); } } } // 广度优先遍历 void bfs(int cur, vector<bool>& visited) { queue<int> q; q.push(cur); visited[cur] = true; while (!q.empty()) { int u = q.front(); q.pop(); cout << u << " "; for (auto e : adj[u]) { if (!visited[e.to]) { visited[e.to] = true; q.push(e.to); } } } } }; int main() { int n, m; cin >> n >> m; Graph g(n); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; g.addEdge(u, v, w); g.addEdge(v, u, w); // 无向图需要添加反向边 } // 深度优先遍历 cout << "DFS: "; vector<bool> visited(n, false); g.dfs(0, visited); cout << endl; // 广度优先遍历 cout << "BFS: "; visited.assign(n, false); g.bfs(0, visited); cout << endl; return 0; }给代码添加注释
```
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// 定义图的边结构体
struct Edge {
int to; // 边的终点
int weight; // 边的权重
Edge(int t, int w) : to(t), weight(w) {} // 构造函数
};
// 定义图的邻接表结构体
struct Graph {
int n; // 节点数
vector<vector<Edge>> adj; // 邻接表
Graph(int n) : n(n), adj(n) {} // 构造函数
// 添加一条边
void addEdge(int from, int to, int weight) {
adj[from].push_back(Edge(to, weight));
}
// 深度优先遍历
void dfs(int cur, vector<bool>& visited) {
visited[cur] = true;
cout << cur << " ";
for (auto e : adj[cur]) {
if (!visited[e.to]) {
dfs(e.to, visited);
}
}
}
// 广度优先遍历
void bfs(int cur, vector<bool>& visited) {
queue<int> q;
q.push(cur);
visited[cur] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
cout << u << " ";
for (auto e : adj[u]) {
if (!visited[e.to]) {
visited[e.to] = true;
q.push(e.to);
}
}
}
}
};
int main() {
int n, m;
cin >> n >> m;
Graph g(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
g.addEdge(u, v, w);
g.addEdge(v, u, w); // 无向图需要添加反向边
}
// 深度优先遍历
cout << "DFS: ";
vector<bool> visited(n, false);
g.dfs(0, visited);
cout << endl;
// 广度优先遍历
cout << "BFS: ";
visited.assign(n, false);
g.bfs(0, visited);
cout << endl;
return 0;
}
```
注释已添加在代码中。
#include <iostream> #include <vector> using namespace std; // 定义边结构体 struct Edge { int dest; // 目标顶点 int weight; // 边的权值 Edge(int d, int w): dest(d), weight(w) {} }; // 定义邻接表节点结构体 struct Node { int vertex; // 顶点编号 vector<Edge> edges; // 与该顶点相连的边 Node(int v): vertex(v) {} }; // 邻接表表示法构造无向网G vector<Node> buildGraph() { vector<Node> graph; int numNodes, numEdges; cout << "请输入顶点数和边数:"; cin >> numNodes >> numEdges; graph.resize(numNodes); cout << "请输入每条边的起点、终点和权值:" << endl; for (int i = 0; i < numEdges; ++i) { int start, dest, weight; cin >> start >> dest >> weight; graph[start].edges.emplace_back(dest, weight); graph[dest].edges.emplace_back(start, weight); } return graph; } // 递归地深度优先遍历该图G void dfs(vector<Node>& graph, vector<bool>& visited, int current) { visited[current] = true; cout << current << " "; for (Edge& e : graph[current].edges) { int next = e.dest; if (!visited[next]) { dfs(graph, visited, next); } } } int main() { vector<Node> graph = buildGraph(); vector<bool> visited(graph.size(), false); int start; cout << "请输入起始顶点编号:"; cin >> start; dfs(graph, visited, start); cout << endl; return 0; }运行一下这段代码
当你运行这段代码时,程序会提示你输入顶点数和边数,你需要输入两个整数。然后程序会提示你输入每条边的起点、终点和权值,你需要按照提示输入每条边的起点、终点和权值,其中起点和终点都是整数,权值可以是整数或浮点数。输入完所有边后,程序会要求你输入起始顶点编号,你需要输入一个整数。程序会从该顶点开始递归地深度优先遍历整个图,并输出遍历结果,每个遍历到的顶点编号后面会跟一个空格。最后程序会输出一个换行符,表示遍历结束。