静态链表储存

</pre><pre name="code" class="cpp">#include <iostream>
#include <queue>
using namespace std;
const long edge_maxn = 1005;  //边的最大上限
const long point_maxn = 105;  //点的最大上限
struct node
{/*node存储边,一个edge代表一条边*/
	int v;  //终点位置
	int w;  //权值
	int next;  //同一起点的下一条边存储在edge数组中的位置(理解了这个静态邻接表就可以了)
}edge[edge_maxn];
int pre[point_maxn];  //以该点为起点的第一条边存储在edge数组中的位置
int n; //点的数量
int m; //边的数量
void Init()
{
	memset(pre,-1,sizeof(pre));
	int Index = 1;
	int i,x,y,w;
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&x,&y,&w);
		
		edge[Index].v = y;
		edge[Index].w = w;
		edge[Index].next = pre[x];  //保存x起点的上一条边在edge数组中的位置
		pre[x] = Index++;  //位置更新
	}
}
void print()
{
	for(int i=1;i<=n;i++)
	{
		printf("%d\n",i);
		for(int j=pre[i];j!=-1;j=edge[j].next)
		{
			printf(" -> %d value is %d\n",edge[j].v,edge[j].w);
		}
		printf("\n");
	}
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF && (n!=0 || m!=0))
	{
		Init();
		print();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值