zoukankan      html  css  js  c++  java
  • 从0开始 图论学习 前向星表示法

    前向星的建立和遍历

    writer:pprp

    注意:从0开始不是从1开始

    链式前向星算法可以对应点特别多的情况,可以存储重复边,但不能直接判断图中任意两点是有边。

    代码如下:

    //writer:pprp
    //前向星表示
    //注意:点的编号也是从0开始的,不能从1开始
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    const int maxn = 1000;
    struct node
    {
        int op,ed,w;
    };
    node e[maxn];
    int head[maxn];
    bool cmp(node n1, node n2)
    {
        if(n1.op == n2.op && n1.ed == n2.ed)
            return n1.w < n2.w;
        if(n1.op == n2.op)
            return n1.ed < n2.ed;
        return n1.op < n2.op;
    }
    int main()
    {
        int vv, ee;
        cin >> vv >> ee;
        for(int i = 0; i < ee; i++)
            cin >> e[i].op >> e[i].ed >> e[i].w;
        sort(e,e+ee,cmp);
        memset(head,-1,sizeof(head));
        head[e[0].op] = 0;
        for(int i = 0 ; i < ee ; i ++)
        {
            if(e[i].op != e[i-1].op)
                head[e[i].op] = i;
        }
    
        for(int i = 0; i < vv; i++)
        {
            for(int k = head[i]; e[k].op == i && k < ee; k++)
            {
                cout <<"op:" << e[k].op << " ed:" << e[k].ed << " w:" << e[k].w << endl;
            }
        }
    
        return 0;
    }
    
    
  • 相关阅读:
    养生之《爱的供养》
    道家养生,与佛家养德
    个人的佛法·5·活着,与死去
    自我囚禁与安住于轮回
    个人的佛法·4·我是不是东西?
    个人的佛法·2
    我们都是生命的灯塔
    大O表示法是什么?
    uni-app商城项目(01)
    2059
  • 原文地址:https://www.cnblogs.com/pprp/p/7786687.html
Copyright © 2011-2022 走看看