链式前向星

· · 算法·理论


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