跳到正文

算法·理论

链式前向星

evajiangwater约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年10月6日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int cnt,head[N];
struct point{
	int to,last,value;
	
};
point maze[N];
void add(int x,int y,int w){
	cnt++;
	maze[cnt].to =y;
	maze[cnt].value = w;
	maze[cnt].last =head[x];
	head[x]=cnt;
}
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y,d;
		cin>>x>>y>>d;
		add(x,y,d);
	}
	cout<<endl;
	for(int i=1;i<=n;i++){
		for(int j=head[i];j!=0;j=maze[j].last ){
			cout<<i<<' '<<maze[j].to <<' '<<maze[j].value <<endl;
		}
	}
	return 0;
}
原文