首页 > 其他 > 详细

链式前向星 模板

时间:2020-09-16 14:34:08      阅读:48      评论:0      收藏:0      [点我收藏+]
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1e3 + 5;
const int MAXM = 2 * 1e3 + 5;

int n, m, head[MAXN], cnt;

struct node {
	int to, dis, next;
} a[MAXM]; 

void AddEdge(int u, int v, int w) {
	a[++cnt].to = v;
	a[cnt].dis = w;
	a[cnt].next = head[u];
	head[u] = cnt; 
}

void visit(int u) {
	for (int i = head[u]; i; i = a[i].next) {
		printf ("%d -> %d :%d\n", u, a[i].to, a[i].dis); 
	}
}

int main() {
	scanf ("%d %d", &n, &m);
	for (int i = 1, u, v, w; i <= m; i ++) {
		scanf ("%d %d %d", &u, &v, &w);
		AddEdge(u, v, w);
		AddEdge(v, u, w);	
	}
	for (int i = 1; i <= n; i++) {
		visit(i);
	} 
	return 0;
} 

链式前向星 模板

原文:https://www.cnblogs.com/cqbz-ChenJiage/p/13678585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!