链式前向星模板

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sugarbliss/article/details/82900394
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int head[maxn],to[maxn],Next[maxn],edge[maxn];
int cnt,n,m;
void add(int x, int y, int w) 
{     
    to[++cnt] = y; edge[cnt] = w;
    Next[cnt] = head[x];   
    head[x] = cnt;       
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        int x, y, w;
        scanf("%d%d%d", &x, &y, &w);
        add(x, y, w);
    }
    //第一层for循环是找每一个点,依次遍历点1~n
    for (int i = 1; i <= m; i++)
    for (int k = head[i]; ~k; k = Next[k]) //第二层for循环是遍历以i为起点的所有边
        cout << i << " " << to[k] << " " << edge[k] << endl;
}

猜你喜欢

转载自blog.csdn.net/sugarbliss/article/details/82900394