交个崔鹏作业题 6-C/c++


#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std;
#define MAX 10
#define INF 0;
typedef char E;
typedef struct GraphMartix{
int vex,edge;
int marixWeight[MAX][MAX];
E data[MAX];
}*Graph;
Graph Create(int v){
Graph g= (Graph)malloc(sizeof(GraphMartix));
g->edge=g->vex=0;
memset(g->marixWeight,0,sizeof(g->marixWeight));
return g;
}
void addVex(Graph g,E v){
g->data[g->vex++]=v;

}
int getIndexByElem(Graph g,E v){
int i =0;
while(g){
if(g->data[i]==v)
return i;
i++;
}
return -1;
}
void addEdge(Graph g,E v1,E v2,int w){
int a=getIndexByElem(g,v1);
int b=getIndexByElem(g,v2);
g->marixWeight[a][b]=w;
g->marixWeight[b][a]=w;
g->edge++;
}
int arr[MAX];
void printMarix(Graph g){
for(int i =0;i<g->vex;i++)
{
int temp=0;
for(int j=0;j<g->vex;j++ ){
//cout<<g->marixWeight[i][j]<<" ";
temp+= g->marixWeight[i][j];
}
arr[i]=temp;
}
}

void Fluid(Graph g){
for(int i =0 ;i <g->vex;i++)
for(int j=0;j<g->vex;j++)
for(int k=0;k<g->vex;k++)
{
if(g->marixWeight[j][i]!=0&&g->marixWeight[i][k]!=0&&j!=k)
{
int newDistance=g->marixWeight[j][i]+g->marixWeight[i][k];
if (newDistance < g->marixWeight[j][k] || g->marixWeight[j][k] == 0)
g->marixWeight[j][k] = newDistance;
}
}


printMarix(g);
}
int main(){
int n;
cin>>n;
Graph graph = Create(n);
char v;
for (int c =0;c<n;c++){
cin>>v;
addVex(graph,v);
}

char a,b;
while(cin>>a>>b){
int w;
cin>>w;
addEdge(graph,a,b,w);
}
Fluid(graph);
int index =0;
for(int i =0;i<n;i++)
if(arr[index]>arr[i])
index=i;
cout<<graph->data[index]<<endl;
}

posted @ 2023-12-19 20:29  Happy_Eric  阅读(8)  评论(0编辑  收藏  举报