链式前向星 模板

#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
", 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;
}