/*Title:Éî¶ÈÓÅÏÈËÑË÷£¬¹ã¶ÈÓÅÏÈËÑË÷Author:LiuXiaodong*/#includestdio.h#includestdlib.h#defineVERTEX_NUM20#defineVERTYPEinttypedefstructArcell{VERTYPEadj;}Arcell,AdjMatrix[VERTEX_NUM][VERTEX_NUM];typedefenum{DG,DN,UDG,UDN}GraphKind;typedefstruct{VERTYPEvexs[VERTEX_NUM];AdjMatrixarcs;intvexnum,arcnum;GraphKindkind;}MGraph;typedefstruct{intdata[100];int*front,*rear;intlength=0;}Queue;voidInit(Queue&Q){for(inti=0;i100;i++)Q.data[i]=0;Q.front=Q.rear=Q.data;}voidEnQueue(Queue&Q,intv){*Q.rear++=v;Q.length++;}voidDeQueue(Queue&Q){Q.rear--;Q.length--;}intQueueEmpty(QueueQ){returnQ.length;}voidvisitFunc(intv){printf(%d,v);}voidDFS(MGraphG,intv,intvisited[]){visited[v]=1;visitFunc(G.vexs[v]);for(intw=1;w=G.vexnum;w++){if(G.arcs[v][w].adj==1&&visited[w]==0){DFS(G,w,visited);}}}voidBFS(MGraphG,intv,intvisited[]){intw=0;QueueQ;Init(Q);visited[v]=1;visitFunc(G.vexs[v]);EnQueue(Q,v);while(QueueEmpty(Q)){DeQueue(Q);for(intn=0;nG.vexnum;n++)for(w=0;wG.vexnum;w++){if(visited[w]==0&&G.arcs[n][w].adj==1){visited[w]=1;visitFunc(G.vexs[w]);EnQueue(Q,w);}}}}intmain(){MGraphG;inti=0,j=0,k=0;intw=0;intl=0,m=0;intvisited[100];printf(ÇëÊäÈ붥µãµÄÊýÄ¿¡¢±ßµÄÊýÄ¿ÒÔ¼°Í¼µÄÀàÐÍ£º\n);scanf(%d%d%d,&G.vexnum,&G.arcnum,&G.kind);printf(ÇëÊäÈ붥µãµÄÊý¾Ý£º\n);while(iG.vexnum)scanf(%d,&G.vexs[i++]);printf(ÇëÊäÈë±ßµÄ¹Øϵ£º\n);for(l=0;lG.arcnum;l++)for(m=0;mG.arcnum;m++)G.arcs[l][m].adj=0;while(kG.arcnum){scanf(%d%d%d,&i,&j,&w);G.arcs[i][j].adj=w;k++;}printf(Éî¶ÈÓÅÏÈËÑË÷µÄ½á¹ûΪ£º\n);for(m=0;mG.vexnum;m++)visited[m]=0;DFS(G,0,visited);printf(\n¹ã¶ÈÓÅÏÈËÑË÷µÄ½á¹ûΪ£º\n);for(m=0;mG.vexnum;m++)visited[m]=0;BFS(G,0,visited);return0;}