[图论] 链式向前星存图

https://blog.csdn.net/Binary_Heap/article/details/78209086

//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 10;

struct EDGE //边
{
    int next;   //下一条边
    int to;     //本边指向节点
    int val;    //权
}edge[MAXN];

int head[MAXN]; //以I为头节点的最后一条边地址 对应edge[I]

int cnt = 0;    //edge计数

void add(int x, int y, int z)
{
	edge[++cnt].next = head[x];
    edge[cnt].val = z;
    edge[cnt].to = y;
    head[x] = cnt;      //以I为头节点的最后一条边地址 对应edge[I] 覆盖更新 保证最后一条
}

void puts()
{
	int st;
	
	cin>>st;
	
	for(int i = head[st]; i != 0; i = edge[i].next) //链式向前星的遍历 从尾至头
	{
		printf("start:%d\n", st);
		printf("end:%d\n", edge[i].to);
		printf("val:%d\n", edge[i].val);
	}
}


int main()
{
    ios::sync_with_stdio(false);

    cin.tie(0);     cout.tie(0);

    int n, m, s, x, y, z;
    
    cin>>n>>m>>s;
    
    for(int i = 0; i < m; i++)
    {
    	cin>>x>>y>>z;
    	
    	add(x, y, z);
	}
	
	puts();

    return 0;
}

Graph(图)

猜你喜欢

转载自blog.csdn.net/Zeolim/article/details/81741443
今日推荐