• 古老的四种图存储方式


    #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);

    }}

  • 相关阅读:
    JOISC2017 手持ち花火 (Sparklers) 题解
    Codeforces 1427E. Xum 题解
    AGC027D
    Codeforces 1416F. Showing Off 题解
    AGC032F
    AGC019E
    TCP实验(前言)
    OS第一次实验截图
    第12组 Beta (2/6)(丁泽中)
    第12组 Beta (1/6)(丁泽中)
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3329937.html
Copyright © 2020-2023  润新知