邻接矩阵:
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 7 typedef char VertexType[4]; 8 typedef int VRtype; 9 #define INF 10000010 #define Max 2011 12 typedef struct /*定点*/13 {14 VRtype adj; /*权值*/15 }ARCNode,AdjMatrix[Max][Max];16 typedef struct /*图类型*/17 {18 VertexType vex[Max]; /*储存定点*/19 AdjMatrix arc; /*邻接矩阵*/20 int vexnum,arcnum; /*定点,边数目*/21 }Mgraph;22 23 void CreateGraph(Mgraph *N);24 void Display(Mgraph N);25 int Locate(Mgraph *N,char *v);26 27 int main()28 {29 Mgraph N;30 CreateGraph(&N);31 Display(N);32 }33 int Locate(Mgraph *N,char *v)34 {35 int i;36 for(i=0;i vexnum;i++)37 if(strcmp(v,N->vex[i])==0)38 return i;39 return -1;40 }41 void CreateGraph(Mgraph *N)42 {43 44 VertexType v1,v2;45 VRtype w;46 cout<<"输入定点,边数目"< >N->vexnum>>N->arcnum;48 int i,j,k;49 cout<<"输入定点名称"< vexnum;i++) /*输入定点名称*/51 cin>>N->vex[i];52 53 for(i=0;i vexnum;i++) /*初始化*/54 {55 for(j=0;j vexnum;j++)56 {57 N->arc[i][j].adj=INF;58 }59 }60 cout<<"输入边权值"< arcnum;k++)62 {63 cin>>v1>>v2>>w;64 i=Locate(N,v1);65 j=Locate(N,v2);66 N->arc[i][j].adj=w;67 }68 }69 void Display(Mgraph N)70 {71 int i,j,k;72 for(i=0;i
2.邻接表
#include#include #include using namespace std;#define Max 20int main(){ vector G[Max]; /*有向图*/ /* struct edge{int to;int cost;} /*带权又向图*/ int v,e; cin>>v>>e; int i,j,k; vector ::iterator it; for(i=0;i >s>>t; G[s].push_back(t); G[t].push_back(s); } for(i=1;i<=v;i++) { for(it=G[i].begin();it!=G[i].end();it++) cout<<*it<<" "; cout<