第二题
#include<stdio.h>
#include<stdlib.h>
#define MAX_VER_NUM 50
#define MAX_ARC_NUM 50
#define INF 9999
typedef char VertexType;
int visited[MAX_VER_NUM];
typedef struct{
VertexType adjvex;
int lowcost;
}ShortEdge;
typedef struct{
VertexType vexs[MAX_VER_NUM];
int arcs[MAX_VER_NUM][MAX_VER_NUM];
int vexnum,arcnum;
}MGraph;
typedef struct{
VertexType start;
VertexType end;
int weight;
}Edge;
int getIndexOfVexs(char vex,MGraph *MG){
int i;
for(i=0;i<MG->vexnum;i++){
if(MG->vexs[i]==vex)
return i;
}
return 0;
}
void SortEdge(Edge E[],int e){
int i,j,k=0;
Edge temp;
for(i=1;i<e;i++){
temp=E[i];
j=i-1;
while(j>=0&&temp.weight<E[j].weight)
{
E[j+1]=E[j];
j--;
}
E[j+1]=temp;
}
}
void create_MG(MGraph *MG,Edge E[]){
int i,j,k,distance;
int v1,v2,type;
char c1,c2;
printf("请输入城市(顶点)数:");
scanf("%d",&MG->vexnum);
getchar();
printf("请输入路径(边)数:");
scanf("%d",&MG->arcnum);
getchar();
for(i=0;i<MG->vexnum;i++){
printf("请输入第%d个城市:",i+1);
scanf("%c",&MG->vexs[i]);
getchar();
}
for(i=0;i<MG->vexnum;i++){
for (j=0;j<MG->vexnum;j++)
MG->arcs[i][j]=9999;
}
for(int k=0;k<MG->arcnum;k++){
printf("请输入第%d条边的信息 v(char) v(char) distance(int):",k+1);
scanf("%c %c %d",&c1,&c2,&distance);
getchar();
v1=getIndexOfVexs(c1,MG);
v2=getIndexOfVexs(c2,MG);
E[k].start=v1;
E[k].end=v2;
MG->arcs[v1][v2]=MG->arcs[v2][v1]=distance;
E[k].weight=distance;
}
}
void print_MG(MGraph MG){
int i,j;
printf("城市的列表如下,共有5个城市:\n");
for(i=0;i<MG.vexnum;i++){
printf("(%d)%c\t",i+1,MG.vexs[i]);
}
printf("\n");
for(i=0;i<MG.vexnum;i++){
for(j=0;j<MG.vexnum;j++){
if(i==j) break;
else if(MG.arcs[i][j]==9999){
printf("%c城市到%c城市不能直接到达\n",MG.vexs[i],MG.vexs[j]);
}
else{
printf("%c城市到%c城市距离%d万里\n",MG.vexs[i],MG.vexs[j],MG.arcs[i][j]);
}
}
}
}
void DFS(MGraph *MG, int i){
int j;
printf("%c",MG->vexs[i]);
visited[i] = 1;
for (j=0;j<MG->vexnum;j++){
if (MG->arcs[i][j] != 9999 && visited[j]==0){
DFS(MG,j);
}
}
}
void DFSTraverse(MGraph *MG){
int i,count=0;
for (i=0;i<MG->vexnum;i++){
visited[i] = 0;
}
for (i=0;i<MG->vexnum;i++){
if (visited[i]==0){
DFS(MG, i);
count++;
}
}
printf("%d",count);
}
int min(MGraph *MG,ShortEdge *shortedge){
int i,j;
int min,loc;
min=9999;
for(i=1;i<MG->vexnum;i++)
{
if(min>shortedge[i].lowcost&&shortedge[i].lowcost!=0)
{
min=shortedge[i].lowcost;
loc=i;
}
}
return loc;
}
int father[MAX_VER_NUM];
int findFather(int x){
int a=x;
while(x != father[x]){
x=father[x];
}
while(a!=father[a]){
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Prim(MGraph *MG,VertexType start){
int i,j,k,count=0;
ShortEdge shortedge[MAX_VER_NUM];
k=getIndexOfVexs(start,MG);
for(i=0;i<MG->vexnum;i++)
{
shortedge[i].adjvex=start;
shortedge[i].lowcost=MG->arcs[k][i];
}
shortedge[k].lowcost=0;
for(i=0;i<MG->vexnum-1;i++)
{
k=min(MG,shortedge);
printf("构成最小代价生成树的边集为:");
printf("%c->%c,%d\n",shortedge[k].adjvex,MG->vexs[k],shortedge[k].lowcost);
count+=shortedge[k].lowcost;
shortedge[k].lowcost=0;
for(j=0;j<MG->vexnum;j++)
{
if(MG->arcs[k][j]<shortedge[j].lowcost)
{
shortedge[j].lowcost=MG->arcs[k][j];
shortedge[j].adjvex=MG->vexs[k];
}
}
}
printf("用Prim生成最小生成树权值之和为:%d\n",count);
}
void Kruskal(MGraph *MG,Edge E[]){
int i,j,k,start1,end1,sn1,sn2,count=0;
int vset[MAX_VER_NUM];
for(int i=0;i<MG->vexnum;i++){
father[i]=i;
}
SortEdge(E,MG->arcnum);
for(i=0;i<MG->vexnum;i++) vset[i]=i;
k=1;
j=0;
while(k<MG->vexnum)
{
start1=E[j].start;
end1=E[j].end;
sn1=vset[start1];
sn2=vset[end1];
if(sn1!=sn2){
printf("构成最小代价生成树的边集为:");
printf("边(%d,%d),权值为%d\n",start1,end1,E[j].weight);
count+=E[j].weight;
k++;
for(i=0;i<MG->vexnum;i++){
if(vset[i]==sn2){
vset[i]=sn1;
}
}
}
j++;
}
printf("用Krusal生成最小生成树权值之和为:%d\n",count);
}
char in_point()
{
char start;
printf("请输入起始点:");
scanf("%c",&start);
return start;
}
int main(void){
int i,j;int a;
MGraph MG;
Edge E[MAX_VER_NUM];
while(1){
printf("\n功能列表\n");
printf("--------------------\n");
printf("1.输入城市信息;\n");
printf("2.打印城市信息和城市之间的直接可达距离;\n");
printf("3.DFS遍历;\n");
printf("4.Prime算法实现最小生成树;\n");
printf("5.Kruskal算法实现最小生成树;\n");
printf("6.退出;\n");
printf("--------------------\n");
printf(">>>");
scanf("%d",&a);
switch(a){
case 1: create_MG(&MG,E);break;
case 2: print_MG(MG);break;
case 3: DFSTraverse(&MG);break;
case 4: in_point();Prim(&MG,in_point());break;
case 5: Kruskal(&MG,E);break;
case 6: return 0;
default:printf("选择错误,请重新选择!\n");
}
}
return 0;
}
第三题
#include <stdio.h>
#include <time.h>
#include <string.h>
#define MaxNum 20
#define MaxValue 9999
#define Disconnect 9999
#define MAX_VERTEX_NUM 20
#define INFINITE 9999
#define USED 0;
#define NoL -1
typedef struct{
char Vertex[MaxNum];
int VertexNum;
int EdgeNum;
int EdgeWeight[MaxNum][MaxNum];
int isTrav[MaxNum];
}GraphMatrix;
typedef struct {
int vertexNum;
char vertex[MAX_VERTEX_NUM];
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
}Graph,*PGraph;
typedef struct {
int distance;
int path[MAX_VERTEX_NUM];
}ArrayNode;
void CreateGraph(GraphMatrix *GM){
int i,j,k;
int weight;
char EstartV,EendV;
printf("输入图中各顶点信息\n");
for (i = 0; i<GM->VertexNum; i++){
getchar();
printf("第%d个顶点:",i+1);
scanf("%c",&(GM->Vertex[i]));
}
printf("输入构成各边的顶点及权值:\n");
for (k = 0; k <GM->EdgeNum; k++){
getchar();
printf("第%d条边:",k+1);
scanf("%c %c %d",&EstartV,&EendV,&weight);
for ( i= 0; EstartV != GM->Vertex[i]; i++);
for ( j= 0; EendV != GM->Vertex[j]; j++);
GM->EdgeWeight[i][j] = weight;
GM->EdgeWeight[j][i] = weight;
}
}
void ClearGraph(GraphMatrix *GM){
int i,j;
for (i=0; i<GM->VertexNum; i++){
for (j=0; j<GM->VertexNum; j++){
GM->EdgeWeight[i][j] = MaxValue;
}
}
}
void OutGraph(GraphMatrix *GM){
int i,j;
for (j=0; j<GM->VertexNum; j++);
printf("\n");
printf("\n城市的列表如下,共有%d个城市:\n",j);
for (j=0; j<GM->VertexNum; j++){
printf("\t%c",GM->Vertex[j]);
}
printf("\n");
for ( i=0; i<GM->VertexNum; i++){
for (j=0; j<GM->VertexNum; j++){
if (GM->EdgeWeight[i][j] == MaxValue){
if(i==j)continue;
printf("%c到%c不能直接到达\n",GM->Vertex[i],GM->Vertex[j]);
}
else{
printf("%c到%c距离%d",GM->Vertex[i],GM->Vertex[j],GM->EdgeWeight[i][j]);
printf("\n");
}
}
}
}
GraphMatrix iocity(GraphMatrix GM){
printf("\n生成无向图!\n");
printf("输入图的顶点数量:");
scanf("%d",&GM.VertexNum);
printf("输入图的边数量:");
scanf("%d",&GM.EdgeNum);
ClearGraph(&GM);
CreateGraph(&GM);
return GM;
}
void createdGraph(PGraph g,GraphMatrix GM) {
int i,j;
g->vertexNum=GM.VertexNum;
for(i=0;i<g->vertexNum;i++)
g->vertex[i]='A'+i;
for(i=0;i<g->vertexNum;i++)
for(j=0;j<g->vertexNum;j++)
g->arc[i][j]=0;
for(int a=0;a<GM.VertexNum;a++){
for(int b=0;b<GM.VertexNum;b++){
if(GM.EdgeWeight[a][b]==MaxValue)continue;
g->arc[a][b]=GM.EdgeWeight[a][b];
}
}
}
void Dijkstra(int from,int to,PGraph g) {
char aa,bb;
aa=(char)(from+65);
bb=(char)(to+65);
int i,j,index=-1;
int n=1;
ArrayNode shortestPath[MAX_VERTEX_NUM];
int flag[MAX_VERTEX_NUM]={0};
for(i=0;i<g->vertexNum;i++){
if(from==i){
shortestPath[i].distance=0;
shortestPath[i].path[0]=i;
flag[from]=1;
}
else if(g->arc[from][i]>0){
shortestPath[i].path[0]=from;
shortestPath[i].path[1]=i;
shortestPath[i].distance=g->arc[from][i];
}else
shortestPath[i].distance=INFINITE;
}
while(n<g->vertexNum){
index=-1;
for(i=0;i<g->vertexNum;i++){
if(i==from)
continue;
if(flag[i]==0 && index==-1 && shortestPath[i].distance!=INFINITE)
index=i;
if(flag[i]==0 && index!=-1 && shortestPath[i].distance<shortestPath[index].distance)
index=i;
}
flag[index]=1;
for(i=0;i<g->vertexNum;i++){
if(i==from)
continue;
if(g->arc[index][i]>0 && g->arc[index][i]+shortestPath[index].distance<shortestPath[i].distance){
shortestPath[i].distance=g->arc[index][i]+shortestPath[index].distance;
j=0;
while(1){
shortestPath[i].path[j]=shortestPath[index].path[j];
if(shortestPath[index].path[j]==index)
break;
j++;
}
shortestPath[i].path[j+1]=i;
}
}
n++;
}
if(shortestPath[to].distance==INFINITE){
printf("%c到%c没有路径\n",aa,bb);
return;
}
printf("%c到%c的最短路径长度是:%d\n",aa,bb,shortestPath[to].distance);
printf("经过的顶点: ");
i=0;
while(1){
printf("%-3c",shortestPath[to].path[i]+'A');
if(shortestPath[to].path[i]==to)
break;
i++;
}
printf("\n");
}
void ShortPath(GraphMatrix GM) {
Graph g;
char from,to,BBB;
int a,b;
createdGraph(&g,GM);
printf("请输入起点终点(如AB,中间不要有空格):");
scanf("%c%c%c",&BBB,&from,&to);
a=(int)from;
b=(int)to;
Dijkstra((a-65),(b-65),&g);
}
int main(){
GraphMatrix GM;
int choose, k;
do{
printf("\n请选择要执行的模块\n\n");
printf("1. 输入城市信息\n");
printf("2. 打印城市信息和城市之间的直接可达距离\n");
printf("3. 计算任意两个城市之间的最短路径\n");
printf("4. 退出\n\n");
scanf("%d",&choose);
switch(choose){
case 1: GM = iocity(GM);break;
case 2: OutGraph(&GM);break;
case 3: ShortPath(GM);break;
case 4: return 0;
default: printf("选择错误,重新选择\n");
}
}while(1);
}