#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
#define MaxValue 32700
const int NumEdges = 50;
const int NumVertices = 10;
typedef char VertexData;
typedef int EdgeData;
typedef struct {
VertexData vexList[NumVertices];
EdgeData Edge[NumVertices][NumVertices];
int n, e;
} MTGraph;
int visited[100];
queue<int>Q;
void BFS(MTGraph G,int v) {
Q.push(v);
int t;
while(!Q.empty()) {
t=Q.front();
Q.pop();
visited[t]=1;
cout<<t<<" ";
for(int i=1; i<=G.n; i++) {
if( (!visited[i]) && G.Edge[t][i]<MaxValue) Q.push(i);
}
}
}
int main() {
MTGraph G;
int s,t,path;
cout<<"请输入顶点个数"<<endl;
cin>>G.n;
cout<<"请输入边数"<<endl;
cin>>G.e;
for(int i=1; i<=G.n; i++) {
for(int j=1; j<=G.n; j++) {
G.Edge[i][j]=MaxValue;
}
}
cout<<"请输入各边权值"<<endl;
for(int i=1; i<=G.e; i++) {
cin>>s>>t>>path;
G.Edge[s][t]=path;
}
for(int i=1; i<=G.n; i++) {
visited[i]=0;
}
BFS(G,1);
cout<<endl;
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;
#define MAX_VERTEX_NUM 20
const int NumEdges = 50;
const int NumVertices = 10;
typedef char VertexData;
typedef int EdgeData;
typedef struct node {
int dest;
EdgeData cost;
struct node * link;
} EdgeNode;
typedef struct {
VertexData data;
EdgeNode * firstAdj;
} VertexNode;
typedef struct {
VertexNode VexList [NumVertices];
int n, e;
} AdjGraph;
void CreateGraph (AdjGraph G) {
int tail,head,weight;
cout<<"请输入顶点个数和边数"<<endl;
scanf ("%d %d", &G.n, &G.e);
cout<<"请输入顶点信息"<<endl;
for ( int i = 0; i < G.n; i++) {
scanf("%c",&G.VexList[i].data );
G.VexList[i].firstAdj = NULL;
}
cout<<"请输入边"<<endl;
for ( int i = 0; i < G.e; i++) {
scanf ("%d %d %d",&tail,&head,&weight );
EdgeNode * p ;
p->dest = head;
p->cost = weight;
p->link = G.VexList[tail].firstAdj;
G.VexList[tail].firstAdj = p;
p = new EdgeNode;
p->dest = tail;
p->cost = weight;
p->link = G.VexList[head].firstAdj;
G.VexList[head].firstAdj = p;
}
}
int main() {
AdjGraph G;
CreateGraph ( G );
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define MaxValue 32700
const int NumEdges = 50;
const int NumVertices = 10;
typedef char VertexData;
typedef int EdgeData;
typedef struct {
VertexData vexList[NumVertices];
EdgeData Edge[NumVertices][NumVertices];
int n, e;
} MTGraph;
int visited[100];
void DFS(MTGraph G,int v){
visited[v]=1;
cout<<v<<" ";
for(int i=1;i<=G.n;i++){
if( (!visited[i]) && G.Edge[v][i]<MaxValue) DFS(G,i);
}
}
int main(){
MTGraph G;
int s,t,path;
cout<<"请输入顶点个数"<<endl;
cin>>G.n;
cout<<"请输入边数"<<endl;
cin>>G.e;
for(int i=1;i<=G.n;i++){
for(int j=1;j<=G.n;j++){
G.Edge[i][j]=MaxValue;
}
}
cout<<"请输入各边权值"<<endl;
for(int i=1;i<=G.e;i++){
cin>>s>>t>>path;
G.Edge[s][t]=path;
}
for(int i=1;i<=G.n;i++){
visited[i]=0;
}
DFS(G,1);
cout<<endl;
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
#define MaxValue 32700
const int NumEdges = 50;
const int NumVertices = 10;
typedef char VertexData;
typedef int EdgeData;
typedef struct {
VertexData vexList[NumVertices];
EdgeData Edge[NumVertices][NumVertices];
int n, e;
} MTGraph;
int visited[100];
int main() {
MTGraph G;
int s,t,path;
cout<<"请输入顶点个数"<<endl;
cin>>G.n;
cout<<"请输入边数"<<endl;
cin>>G.e;
for(int i=1; i<=G.n; i++) {
for(int j=1; j<=G.n; j++) {
if(i==j) G.Edge[i][j]=0;
else G.Edge[i][j]=MaxValue;
}
}
cout<<"请输入各边权值"<<endl;
for(int i=1; i<=G.e; i++) {
cin>>s>>t>>path;
G.Edge[s][t]=G.Edge[t][s]=path;
}
for(int i=1; i<=G.n; i++) {
visited[i]=0;
}
for(int i=1; i<=G.n; i++) {
for(int j=1; j<=G.n; j++) {
printf("%d\t", G.Edge[i][j]);
}
cout<<endl;
}
system("pause");
cout<<endl;
return 0;
}
#include <stdio.h>
#include <malloc.h>
#include<iostream>
using namespace std;
typedef int InfoType;
#define MAXV 100
#define INF 32767
typedef struct {
int no;
InfoType info;
} VertexType;
typedef struct
{ int edges[MAXV][MAXV];
int n,e;
VertexType vexs[MAXV];
} MGraph;
typedef struct ANode
{ int adjvex;
struct ANode *nextarc;
InfoType info;
} ArcNode;
typedef int Vertex;
typedef struct Vnode
{ Vertex data;
ArcNode *firstarc;
} VNode;
typedef VNode AdjList[MAXV];
typedef struct
{ AdjList adjlist;
int n,e;
} ALGraph;
void MatToList(MGraph g,ALGraph *&G){
int i,j;
ArcNode *p;
G=(ALGraph *)malloc(sizeof(ALGraph));
for (i=0;i<g.n;i++)
G->adjlist[i].firstarc=NULL;
for (i=0;i<g.n;i++)
for (j=g.n-1;j>=0;j--)
if (g.edges[i][j]!=0)
{ p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=g.n;G->e=g.e;
}
void ListToMat(ALGraph *G,MGraph &g){
int i;
ArcNode *p;
for (i=0;i<G->n;i++)
{ p=G->adjlist[i].firstarc;
while (p!=NULL)
{ g.edges[i][p->adjvex]=1;
p=p->nextarc;
}
}
g.n=G->n;g.e=G->e;
}
void DispMat(MGraph g){
int i,j;
for (i=0;i<g.n;i++)
{
for (j=0;j<g.n;j++)
printf("%3d",g.edges[i][j]);
printf("\n");
}
}
void DispAdj(ALGraph *G)
{
int i;
ArcNode *p;
for (i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
cout<<i<<":";
while (p!=NULL)
{
cout<<p->adjvex<<" ";
p=p->nextarc;
}
printf("\n");
}
}
int main()
{
int i,j;
MGraph g,g1;
ALGraph *G;
int m,n;
cin>>n>>m;
int A[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>A[i][j];
}
}
g.n=n;g.e=m;
for (i=0;i<g.n;i++)
for (j=0;j<g.n;j++)
g.edges[i][j]=A[i][j];
G=(ALGraph *)malloc(sizeof(ALGraph));
MatToList(g,G);
DispAdj(G);
}