0
点赞
收藏
分享

微信扫一扫

链式前向星及遍历

//链式前向星存储图,以及遍历
#include <bits/stdc++.h>
using namespace std;
const int M = 5e3 + 10;
const int N = 5e5 + 10;
int head[N];
struct node
{
	int u;
	int v;
	int w;
	int next;
}a[N];
int main()
{
	memset(a, -1, sizeof(a));
	int n, m, u, v, w, cnt=1;
	cout << "请输入有向边的个数 : ";
	cin>> n;
	while (n--)
	{
		cin >> u >> v >> w;
		a[cnt].u = u;
		a[cnt].v = v;
		a[cnt].w = w;
		a[cnt].next = head[u];
		head[u] = cnt++;
	}
	cout << "请输入访问次数 : ";
	cin >> m;
	while (m--)
	{
		cin >> u;
		for (int i = head[u];  a[i].next != -1; i = a[i].next)
		{
			cout << "起点 : "<<a[i].u << "终点 : "<<a[i].v << "权值 : " <<a[i].w << endl;
		}
	}
	return 0;
}

存储一下防丢失

举报

相关推荐

0 条评论