#include<iostream>
using namespace std;
const int MAX = 10;
template<class vertex_type, class edge_weight_type>
class Graph
{
private:
int vertex_num;
int edge_num;
vertex_type vex[MAX];
edge_weight_type arc[MAX][MAX];
public:
Graph();
int locate(char ch);
void print_graph();
};
template<class vertex_type, class edge_weight_type>
int Graph<vertex_type, edge_weight_type>::locate(char ch)
{
int i = 0;
for (i = 0; i < this->vertex_num; i++)
{
if (this->vex[i] == ch)
{
break;
}
}
return i;
}
template<typename vertex_type, typename edge_weight_type>
Graph<typename vertex_type, typename edge_weight_type>::Graph()
{
int i = 0, j = 0, k = 0, weight = 0;
cout << "输入顶点数和边数:" << endl;
cin >> this->vertex_num >> this->edge_num;
cout << "请输入顶点:" << endl;
for (i = 0; i < this->vertex_num; i++)
{
cin >> this->vex[i];
}
for (i = 0; i < this->vertex_num; i++)
{
for (j = 0; j < this->vertex_num; j++)
{
this->arc[i][j] = 0;
}
}
cout << "输入两个顶点及其权值:" << endl;
for (k = 0; k < this->edge_num; k++)
{
char first, second;
cin >> first >> second >> weight;
i = this->locate(first);
j = this->locate(second);
this->arc[i][j] = weight;
this->arc[j][i] = weight;
}
}
template<typename vertex_type, typename edge_weight_type>
void Graph<typename vertex_type, typename edge_weight_type>::print_graph()
{
cout << "邻接矩阵为:" << endl;
for (int i = 0; i < this->vertex_num; i++)
{
for (int j = 0; j < this->vertex_num; j++)
{
cout << "\t" << this->arc[i][j];
}
cout << endl;
}
}
int main()
{
Graph<char, int> g;
g.print_graph();
system("pause");
return 0;
}