本文共 587 字,大约阅读时间需要 1 分钟。
#includeusing namespace std;const int maxn = 1005;//点的最大个数 int n,m,cnt;struct Edge{ int to,w,next;}edge[maxn];int head[maxn];//以i为起点的最后一条边的边的编号 void init(){ for(int i=0;i<=n;i++) head[i] = -1; } void add(int u,int v,int w){ edge[cnt].to = v; edge[cnt].w = w; edge[cnt].next = head[u]; head[u] = cnt++;//编号数加一 }int main(){ cin>>n>>m; int u,v,w; init(); for(int i=1;i<=m;i++) { cin>>u>>v>>w; add(u,v,w); } //遍历 for(int i=1;i<=n;i++) { //n个起点 for(int j=head[i];j!=-1;j = edge[j].next) { cout< <<" "< <<'\n'; } } return 0;}
转载地址:http://nirq.baihongyu.com/