zoukankan      html  css  js  c++  java
  • 邻接表结构

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #define MAX 100010   /*点数*/
     5 int First[MAX]; /*First[x]:x表示头结点为x,First[x]表示下一条边的编号*/
     6 using namespace std;
     7 struct edge
     8 {
     9     int TO;     /*下一个顶点*/
    10     int Next;   /*记录下一条边的编号*/
    11     int Vlaue;  /*权值*/
    12 }ID[3*MAX];   /*边表,无向图的边数记得多弄些*/
    13 
    14 int SIGN;/*链表的边数,链表的边数=无向图边数*2=有向图边数*/
    15 
    16 void Add_E(int x,int y,int z)   /*添加边*/
    17 {
    18     ID[SIGN].TO=y;
    19     ID[SIGN].Vlaue=z;
    20     ID[SIGN].Next=First[x];
    21     First[x]=SIGN++;
    22 }
    23 
    24 void FIND(int x)/*查找与X相连的顶点*/
    25 {
    26     for(i=First[x];i!=0;i=ID[i].Next)   //查找与该点相关的点
    27     {
    28         /*每一个ID[i].TO都是与点x相连的顶点*/
    29         /*每一个ID[i].Vlaue是该边的权值*/
    30         /*...*/
    31     }
    32 }
    33 
    34 int main()
    35 {
    36    int N,M;
    37    int x,y,z;
    38    while(scanf("%d %d",&N,&M)!=EOF)
    39    {
    40         SIGN=1;
    41         for(i=1;i<=N;i++)First[i]=0;
    42         for(i=1;i<=M;i++)
    43         {
    44             scanf("%d %d %d",&x,&y,&z);
    45             Add_E(x,y,z);
    46             /*Add_E(y,x,z);如果是构建无向图的话*/
    47         }
    48    }
    49    return 0;
    50 }
    View Code
    转载请备注:
    **************************************
    * 作者: Wurq
    * 博客: https://www.cnblogs.com/Wurq/
    * Gitee: https://gitee.com/wurq
    **************************************
  • 相关阅读:
    严援朝座右铭
    王治郅 请让爱国走下神坛
    Java 事件处理实例
    SAP ERP 与 Oracle ERP 比较
    Gantt Component for Delphi Pure Pascal code(TsyGantt VCL)
    XMLRPC vs. SOAP
    Interfaces with Constants Only(java中通用常量定义)
    船舶设计软件简介
    DelphiARX 2000i 简介
    JAVA事件适配器用内部类,匿名类实现事件处理
  • 原文地址:https://www.cnblogs.com/Wurq/p/4463156.html
Copyright © 2011-2022 走看看