#include<iostream>
using namespace std;
#define MAX 500
#define INT 999
typedef struct
{char vex[MAX];int Edge[MAX][MAX];int vexnum,arcnum;
}MGraph;
void InitMG(MGraph &MG)
{cout<<"输入顶点数和边数:";cin>>MG.vexnum>>MG.arcnum;cout<<"输入顶点:";for(int i=1;i<=MG.vexnum;i++)cin>>MG.vex[i];for(int i=1;i<=MG.vexnum;i++){for(int j=1;j<=MG.vexnum;j++)MG.Edge[i][j]=INT;}for(int i=1;i<=MG.arcnum;i++){int x,y;cout<<"输入顶点间的关系:";cin>>x>>y;int weight;cout<<"输入顶点间的权值:";cin>>weight;MG.Edge[x][y]=weight;}
}
void Print(MGraph MG)
{cout<<"顶点的个数:"<<MG.vexnum<<endl;cout<<"顶点的边数:"<<MG.arcnum<<endl;cout<<"顶点之间的关系:"<<endl;for(int i=1;i<=MG.vexnum;i++){for(int j=1;j<=MG.vexnum;j++){cout<<MG.Edge[i][j]<<" ";}cout<<endl;}
}
void Dijstra(int v,int *dist,int *prev,MGraph MG)
{bool a[MG.vexnum+1];for(int i=1;i<=MG.vexnum;i++){dist[i]=MG.Edge[v][i];a[i]=false;if(dist[i]==INT)prev[i]=0;elseprev[i]=v;}dist[v]=0;a[v]=true;for(int i=1;i<MG.vexnum;i++){int temp=INT;int u=v;for(int j=1;j<=MG.vexnum;j++){if(temp>dist[j]&&a[j]==false){temp=dist[j];u=j;}}a[u]=true;for(int j=1;j<=MG.vexnum;j++){int sum=dist[u]+MG.Edge[u][j];if(a[j]==false&&dist[j]>sum){dist[j]=sum;prev[j]=u;}}}
}
void Traceback(int v,int e,int prev[])
{if(v==e){cout<<v;return ;}Traceback(v,prev[e],prev);cout<<"->"<<e;
}
int main()
{MGraph MG;InitMG(MG);Print(MG);cout<<"输入源点:";int v;cin>>v;cout<<"结束位置:";int e;cin>>e;int dist[MG.vexnum+1]={0};int prev[MG.vexnum+1]={0};Dijstra(v,dist,prev,MG);cout<<"最短路径长度:"<<dist[e];cout<<"从源点"<<v<<"到结束位置"<<e<<"路径为:";Traceback(v,e,prev);return 0;
}