#include <iostream>
using namespace std;
int first[2505] , next[6505] , top = 0 , worth[6505] , to[6505];
void add()
{
int u , v , w ;
cin >> u >> v >> w;
next[++top] = first[u];
first[u] = top;
to[top] = v;
worth[top] = w;
return ;
}
int main ()
{
int n , m;
cin >> n >> m;
for(int i = 1 ; i <= m ; i++)
{
add();
}
for(int i = 1 ; i <= n ; i++)
{
cout << i << ": ";
for(int e = first[i] ; e ; e = next[e])
{
cout << to[e]<< " ";
}
cout << endl;
}
return 0;
}
前向星(邻接表)
最新推荐文章于 2024-09-05 20:10:19 发布
本文展示了一个使用C++实现的图论算法案例,通过邻接表存储无向图,并完成了边的添加功能。该算法能够接收用户输入的顶点和边数,随后逐条添加边,最终遍历并打印每个顶点的邻接顶点及其权值。
1389

被折叠的 条评论
为什么被折叠?



