临界表和邻接矩阵DFS,BFS,Dijkstra

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define MaxInt 32767
#define  MVNum 100
#define MAX_VERTEX_NUM 20
#define clear(a,b) memset(a,b,sizeof(a))
int vist[10000];
int dist[10000];
int n;
using namespace std;
typedef char VertexType;
typedef int ArcType;
typedef struct {
    VertexType vex[MVNum];
    ArcType arcs[MVNum][MVNum];
    int vexnum,acrnum;
}AMGraph;
typedef  struct ArcNode{
    int adjvex;
    struct ArcNode *nextarcs;//指向下一条弧线
    //InfoArc *info;
}ArcNode;
typedef struct Vnode{
    VertexType data;
    ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct {
    AdjList Vertics;//临界表
    int vexnum,arcnum;//顶点数和边数
    int kind; //图的种类
}ALGraph,*ALGraphList;
typedef struct LoopQueue{
	int data[1000];
	int front,rear;
}LoopQueue,*Queue;
int Locate(AMGraph G,char v){
    for(int i=0;i<G.vexnum;i++){
        if(v==G.vex[i])
            return i;
    }
    return -1;
}
int CreateUdN(AMGraph &G){
    cin>>G.vexnum>>G.acrnum;
    n=G.vexnum;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vex[i];
    }
    for(int i=0;i<G.vexnum;i++){
        for(int j=0;j<G.vexnum;j++){
            G.arcs[i][j]=MaxInt;
        }
    }
    for(int i=0;i<G.acrnum;i++){
        char v1,v2;
        int w;
        cin>>v1>>v2>>w;
        int q=Locate(G,v1);
        int k=Locate(G,v2);
        G.arcs[q][k]=w;
        G.arcs[k][q]=G.arcs[q][k];
    }
    return 0;
}
int Locate(ALGraph G,int v1){
    for(int i=0;i<G.vexnum;i++){
    	if(G.Vertics[i].data==v1){
    		return i;
		}
	}
	return -1;
}
int CreatUDG(ALGraph &G){
    cin>>G.vexnum>>G.arcnum;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.Vertics[i].data;
        G.Vertics[i].firstarc=NULL;
    }
    for(int i=0;i<G.arcnum;i++){
            int v1,v2;
        cin>>v1>>v2;
        //int q=Locate(G,v1);
        //int w=Locate(G,v2);
        ArcNode *p1 = new ArcNode;
        p1->adjvex=v2;
        p1->nextarcs=G.Vertics[v1].firstarc;
        G.Vertics[v1].firstarc=p1;
        ArcNode *p2 = new ArcNode;
        p2->adjvex=v1;
        p2->nextarcs=G.Vertics[v2].firstarc;
        G.Vertics[v2].firstarc=p2;
    }
    return 0;
}
void DFS(ALGraph G,int i){
	ArcNode *p;
	vist[i]=1;
	cout<<G.Vertics[i].data;
	p=G.Vertics[i].firstarc;
	while(p){
		if(!vist[p->adjvex])
		DFS(G,p->adjvex);
		p=p->nextarcs;
	}
}
void initQueue(Queue &Q){
	Q->front=Q->rear=0;
}
bool QueueEmpty(Queue Q){
	if(Q->front==Q->rear) return true;
	return false;
}
bool FullQueue(Queue &Q){
	if((Q->rear+1)%1000==Q->front)
	return true;
	return false;
}
void EnQueue(Queue &Q,int e){
	if(!FullQueue(Q)){
		Q->data[Q->rear]=e;
		Q->rear=(Q->rear+1)%1000;
	}
}
void Dequeue(Queue &Q,int *e){
	if(!QueueEmpty(Q)){
		*e=Q->data[Q->front];
		Q->front=(Q->front+1)%1000;
	}
}
void BFS(ALGraph &G){
	int i;
	Queue Q=(Queue)malloc(sizeof(LoopQueue));
	for(int i=0;i<G.vexnum;i++){
		vist[i]=0;
	}
	initQueue(Q);
	for(int i=0;i<G.vexnum;i++){
		if(!vist[i]){
			vist[i]=1;
			cout<<G.Vertics[i].data;
			EnQueue(Q,i);
		}
		while(!QueueEmpty(Q)){
			Dequeue(Q,&i);
			ArcNode *p=G.Vertics[i].firstarc;
			while(p){
				if(!vist[p->adjvex]){
					vist[p->adjvex]=1;
					cout<<G.Vertics[p->adjvex].data;
					EnQueue(Q,p->adjvex);
				}
			}
		}
	}
}
void init(){
    cout<<"1---邻接矩阵建图"<<endl;
    cout<<"2---临街表建图--"<<endl;
    cout<<"3---DFS便遍历图-"<<endl;
    cout<<"4---BFS遍历图---"<<endl;
    cout<<"5---Dijkstra算法"<<endl;
}
int Dijkstra(AMGraph &G){
     clear(dist,MaxInt);
     clear(vist,0);
    cout<<n<<endl;
    vist[0]=1;
    dist[0]=0;
    int sum=0;
    for(int i=0;i<n;i++){
            int v=-1,maxx=MaxInt;
            for(int j=0;j<n;j++){
                if(!vist[j]&&dist[j]<maxx){
                    maxx=dist[j];
                    vist[j]=1;
                    v=j;
                }
            }
            sum+=maxx;
            vist[v]=1;
            if(v==-1)
                break;
                for(int k=0;k<n;k++){
                    if(!vist[k]&&G.arcs[v][k]+maxx<dist[k]){
                        dist[k]=G.arcs[v][k]+maxx;
                    }
                }
    }
    return sum;
}
int main(){
    int n;
    init();
    ALGraph G;
    AMGraph G1;
    while(1){
            cin>>n;
    if(n==1){
    CreatUDG(G);
    memset(vist,0,sizeof(vist));
    }
    else if(n==2){
    CreateUdN(G1);
    for(int i=0;i<G1.vexnum;i++){
        for(int j=0;j<G1.vexnum;j++){
            cout<<G1.arcs[i][j]<<" ";
        }
        cout<<endl;
    }
    }
    else if(n==3)
    {
        DFS(G,0);
    }
    else if(n==4){
        BFS(G);
    }
    else if(n==5){
        Dijkstra(G1);
    }
    }
  //  BFS(G);
    return 0;
}

 

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/qq_41453511/article/details/84986548
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞