月球美容计划之图的储存结构汇总

月球美容计划之图的储存结构汇总

大家好,又见面了,我是全栈君。

SJ图论非常流弊,为了省赛队里知识尽量广,我就直接把图continue,如今回想起来丫的全忘了,从头開始吧。

先写写图的存储,再写写最小生成树和最短路的几个经典算法,月球美容计划就能够结束了。0 0。拖了好久,还有非常多内容要写。- –

这次总结了邻接矩阵,邻接表。十字链表。邻接多重表,边集数组,这5种经常使用的图的储存结构,或许能当模板用吧。

 

 

邻接矩阵

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//邻接矩阵
int G[100][100];
int add1 (int i,int j,int w)
{
    G[i][j] = w;
	return 0;
}

int main()
{
    int i,n;
    
    //建图
	scanf ("%d",&n);
    for (i = 0;i < n;i++)
    {
        int a,b,w;
        //输入起点、终点、权重
        scanf ("%d%d%d",&a,&b,&w);
        add1 (a,b,w);
        //无向图加上
        add1 (b,a,w);
    }
    return 0;
}

邻接表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//邻接表

struct dot
{
    int d;
    int w;
    struct dot *next;
};

struct hed
{
    int v;
    struct dot *next;
}head[100];

int add2(int i,int j,int w)
{
    struct dot * p;
    struct dot * t = new dot;

    t->d = j;
    t->w = w;
    t->next = NULL;

    if (head[i].next == NULL)
    {
        head[i].next = t;
        return 0;
    }

    p = head[i].next;

    while (p->next != NULL)
        p = p->next;

    p->next = t;

    return 0;
}

int main()
{
    int i,n;
	
	memset (head,0,sizeof (head));
    //建图
    scanf ("%d",&n);
    for (i = 0;i < n;i++)
    {
        int a,b,w;
        //输入起点、终点、权重
        scanf ("%d%d%d",&a,&b,&w);
        add2 (a,b,w);
        //无向图加上
        add2 (b,a,w);
    }
    return 0;
}

十字链表(有向图好用)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//十字链表

struct dot
{
    int d;
    int w;
    struct dot *next;
};

struct hed
{
    int v;
    struct dot *to;
    struct dot *next;
}head[100];

int add3(int i,int j,int w)
{
    struct dot * p;
    struct dot * t = new dot;

    t->d = j;
    t->w = w;
    t->next = NULL;

    //正邻接表构建
    if (head[i].next == NULL)
    {
        head[i].next = t;
    }else
    {
        p = head[i].next;

        while (p->next != NULL)
            p = p->next;

        p->next = t;
    }

    //逆邻接表打十字
    if (head[i].to == NULL)
    {
        head[i].to = t;
        return 0;
    }else
    {
        p = head[i].to;

        while (p->next != NULL)
            p = p->next;

        p->next = t;
    }

    return 0;
}

int main()
{
    int i,n;
	
	memset (head,0,sizeof (head));
    //建图
    scanf ("%d",&n);
    for (i = 0;i < n;i++)
    {
        int a,b,w;
        //输入起点、终点、权重
        scanf ("%d%d%d",&a,&b,&w);
        add3 (a,b,w);
    }
    return 0;
}

邻接多重表(无向图)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//邻接多重表(无向图)

struct dot
{
    int i,j;
    int w;
    struct dot *inext;
    struct dot *jnext;
};

struct hed
{
    int v;
    struct dot *next;
}head[100];

int add4(int i,int j,int w)
{
    struct dot *t = new dot;
    struct dot *p = NULL,*tp = NULL;

    t->i = i;
    t->j = j;
    t->w = w;
    t->inext = NULL;
    t->jnext = NULL;

    if (head[i].next == NULL)
    {
        head[i].next = t;
    }else
    {
        p = head[i].next;

        while (p != NULL)
        {
            tp = p;
            if (p->i == i)
                p = p->inext;
            else
                p = p->jnext;
        }

            if (tp->i == i)
                tp->inext = t;
            else
                tp->jnext = t;
    }

    if (head[j].next == NULL)
    {
        head[j].next = t;
    }else
    {
        p = head[j].next;

        while (p != NULL)
        {
            tp = p;
            if (p->i == j)
                p = p->inext;
            else
                p = p->jnext;
        }

        if (tp->i == j)
                tp->inext = t;
            else
                tp->jnext = t;
    }

    return 0;
}

int main()
{
    int i,n;

    memset (head,0,sizeof (head));
    //建图
    scanf ("%d",&n);
    for (i = 0;i < n;i++)
    {
        int a,b,w;
        //输入起点、终点、权重
        scanf ("%d%d%d",&a,&b,&w);
        add4 (a,b,w);
    }
    return 0;
}

 

边集数组

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//边集数组

struct e
{
    int i,j;
    int w;
    
}eg[100];

int cont;

int add5(int i,int j,int w)
{
    eg[cont].i = i;
    eg[cont].j = j;
    eg[cont].w = w;
    return 0;
}

int main()
{
    int i,n;
	
	memset (eg,0,sizeof (eg));
	cont = 0;
    //建图
    scanf ("%d",&n);
    for (i = 0;i < n;i++)
    {
        int a,b,w;
        //输入起点、终点、权重
        scanf ("%d%d%d",&a,&b,&w);
        //有向图无向图皆可
        add5 (a,b,w);
    }
    return 0;
}

边集数组之前向星

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//前向星
int head[100];

struct e
{
    int to;
    int fro;
    int w;
}eg[100];

int cont;
int add6 (int i,int j,int w)
{
    eg[cont].to = j;
    eg[cont].fro = head[i];
    eg[cont].w = w;
    head[i] = cont++;
	return 0;
}

int main()
{
    int i,n;

    memset (head,-1,sizeof (head));
    cont = 0;
    //建图
	scanf ("%d",&n);
    for (i = 0;i < n;i++)
    {
        int a,b,w;
        //输入起点、终点、权重
        scanf ("%d%d%d",&a,&b,&w);
        add6 (a,b,w);
        //无向图加上
        add6 (b,a,w);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115647.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • stat函数讲解_strcat函数

    stat函数讲解_strcat函数stat函数讲解表头文件:   #include<sys/stat.h>            #include<unistd.h>定义函数:   intstat(constchar*file_name,structstat*buf);函数说明:  &nbs

    2022年8月21日
    4
  • 使用Docker(k8s)安装Kafka并使用宿主机连接

    使用Docker(k8s)安装Kafka并使用宿主机连接使用Docker(k8s)安装Kafka并使用宿主机连接安装Docker及docker-compose具体安装方法可以去官网看教程检查docker-compose是否安装成功创建docker-compose.yml文件version:’2’services:zookeeper:image:”zookeeper”hostname:”zookeeper.local”container_name:”zookeeper”#设置网络别名可随

    2022年8月21日
    4
  • ipconfig配置ip地址(为什么输入ipconfig没有用)

     IP可以分为PublicIP和PrivateIP,出现这种规划的原因在于IPv4所能表示的IP太少而电脑太多以至于不够用,然而只有PublicIP才能直接连接上网络,所以对于那些公司,学校,政府机构等场所,就可以集中使用私有的IP进行管理,而大家可以共用一个IP去连接上公网,这样,就省下了许多宝贵的PublicIP。你有没有发现,你每次使用ipconfig查到的地址,要么就是172….

    2022年4月15日
    83
  • BigDecimal转String[通俗易懂]

    @特别鸣谢:BigDecimal转Stringpublicstaticvoidmain(String[]args){//浮点数的打印System.out.println(newBigDecimal(“10000000000”).toString());//普通的数字字符串System.out.pr…

    2022年4月4日
    5.8K
  • 算法の序列

    算法の序列

    2022年1月4日
    48
  • new construction options_actionsheet

    new construction options_actionsheethttp://www.blogjava.net/lucky/archive/2010/01/19/33380.html 前言1.配置1.1.先决条件1.2.安装1.3.导出过滤器(可选)1.4.安装测试2.概述2.1.引言3.TableTag3.1.引言3.2.显示图片3.3….

    2022年8月20日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号