zoukankan      html  css  js  c++  java
  • 古老的四种图存储方式

    #include <iostream>

    #include <cstdio>

    #include <cstring>

    #include <cstdlib>

    #include <cmath>

    using namespace std;

    int graph[100][100];

    int n,m;

    int x,y,w;

    int main()

    {

        scanf("%d%d",&n,&m);

        for(int i = 1;i <= n;i ++)

          for(int j = 1;j <= n;j ++)

          {

              if(i == j)

                 graph[i][j] = 0;

              else

                 graph[i][j] = 1000000;

          }

        while(m --)

        {

            scanf("%d%d%d",&x,&y,&w);

            graph[x][y] = w;

        }

        for(int i = 1;i <= n;i ++)

           for(int j = 1;j <= n;j ++)

              if(graph[i][j] != 0 && graph[i][j] != 1000000)

                 printf("%d -> %d have %d longth ",i,j,graph[i][j]);

    #include <iostream>

    #include <cstdio>

    #include <cstring>

    #include <cstdlib>

    #include <cmath>

    #include <algorithm>

    using namespace std;

    struct Node

    {

        int from;

        int to;

        int w;

    };

    Node edge[10000];

    int head[10000];

    int n,m;

    int x,y,w;

    bool cmp(Node a,Node b)

    {

        if(a.from == b.from && a.to == b.to)

            return a.w < b.w;

        if(a.from == b.from)

            return a.to < b.to;

        return a.from < b.from;

    }

    int main()

    {

        scanf("%d%d",&n,&m);

        for(int i = 1;i <= m;i ++)

        {

            scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].w);

        }

        sort(edge + 1,edge + m,cmp);

        memset(head,-1,sizeof(head));

        head[edge[0].from] = 0;

        for(int i = 1;i < m;i ++)

        if(edge[i].from != edge[i - 1].from)

            head[edge[i].from] = i;

        for(int i = 1;i <= n;i ++)

        {

           for(int k = head[i];edge[k].from == i && k < m;k ++)

               printf("%d -> %d %d ",edge[k].from,edge[k].to,edge[k].w);

        }

    }}

    #include <iostream>

    #include <cstdio>

    #include <cstring>

    #include <cstdlib>

    #include <cmath>

    #include <algorithm>

    using namespace std;

    struct EdgeNode

    {

        int to;

        int w;

        EdgeNode * next;

    };

    struct VNode

    {

        int from;

        EdgeNode * first;

    };

    VNode Adjlist[1000];

    int n,m;

    int x,y,w;

    int main()

    {

        scanf("%d%d%d",&n,&m);

        while(m --)

        {

            scanf("%d%d%d",&x,&y,&w);

            EdgeNode *p = new EdgeNode();

            p -> to = y;

            p -> w = w;

            p -> next = Adjlist[x].first;

            Adjlist[x].first = p;

        }

        for(int i = 1;i <= n;i ++)

        {

            for(EdgeNode * k;k != NULL;k = k -> next)

            printf("%d %d have %d ",i,k -> to,k -> w);

        }

    #include <iostream>

    #include <cstdio>

    #include <cstring>

    #include <cstdlib>

    #include <cmath>

    #include <algorithm>

    using namespace std;

    int head[10000];

    struct EdgeNode

    {

        int to;

        int w;

        int next;

    };

    EdgeNode Edges[10000];

    int n,m;

    int x,y,w;

    int main()

    {

        scanf("%d%d",&n,&m);

        memset(head,-1,sizeof(head));

        for(int k = 1;k <= m;k ++)

        {

            scanf("%d%d%d",&x,&y,&w);

            Edges[k].to = y;

            Edges[k].w = w;

            Edges[k].next = head[x];

            head[x] = k;

        }

        for(int i = 1;i <= n;i ++)

          for(int k = head[i];k != -1;k = Edges[k].next)

              printf("%d %d have %d ",i,Edges[k].to,Edges[k].w);

    }}

  • 相关阅读:
    Java(14):面向对象、封装、继承、方法重写、多态、抽象类与接口、内部类
    Java(13):数组、Arrays类、冒泡排序
    Java(12):方法、重载、命令行传参、可变参数、方法调用
    Java(11):switch、dowhile、九九乘法表、打印质数、打印三角形
    Java(10):用户交互Scanner
    Java(9):包
    Java(8):运算符
    Java(7):变量和常量及其规范、作用域
    Mybatis 打印日志
    mysql 更新数据
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3329937.html
Copyright © 2011-2022 走看看