#include<iostream>
using namespace std;
#include<vector>
#include<map>
#include<list>
#include<fstream>
#define max 99999
class node {
public:
int node_id;
bool node_remark;
int node_cost;
};
class edge {
public:
int edge_id;
int edge_cost;
int begin_node;
int end_node;
};
class adj {
public:
int adj_id;
int adj_cost;
int adj_node;
bool adj_remark;
};
map<int, vector<adj>>m_adj;
vector<edge>l_edge;
vector<adj>l_adj;
ofstream ofs("adj.txt");
vector<adj> second;
class graph {
public:
adj vertex;
edge Edge;
adj Adj;
//把边的信息插入边容器中
void inputfile() {
ifstream inputfile("linjie.txt");
while (!inputfile.eof()) {
inputfile >>Edge.edge_id >> Edge.begin_node >> Edge.end_node >> Edge.edge_cost;
l_edge.push_back(Edge);
}
}
//输出邻接类
void out_adj() {
for (int i = 0; i < 9; i++) {
for (vector<edge>::iterator it = l_edge.begin(); it != l_edge.end(); it++) {
if (i == it->begin_node) {
Adj.adj_node = it->end_node;
Adj.adj_id = it->edge_id;
Adj.adj_cost = it->edge_cost;
Adj.adj_remark = 0;
l_adj.push_back(Adj);
}
else if (i == it->end_node) {
Adj.adj_node = it->begin_node;
Adj.adj_id = it->edge_id;
Adj.adj_cost = it->edge_cost;
Adj.adj_remark = 0;
l_adj.push_back(Adj);
}
}
m_adj.insert(map<int, vector<adj>>::value_type(i, l_adj));
l_adj.clear();
}
//测试邻接类是否插入正确
for (map<int, vector<adj>>::iterator it = m_adj.begin(); it != m_adj.end(); it++) {
int first = it->first;
second = it->second;
cout << "节点:";
ofs << "节点:";
cout << first << endl;
ofs << first << endl;
cout << "邻接编号" << " " << "邻接节点" << " " << "邻接节点权值" << " " << "邻接节点是否被标记" << " " << endl;
ofs << "邻接编号" << " " << "邻接节点" << " " << "邻接节点权值" << " " << "邻接节点是否被标记" << " " << endl;
for (vector<adj>::iterator it1 = second.begin(); it1 != second.end(); it1++) {
cout << it1->adj_id << " " << it1->adj_node << " " << it1->adj_cost <<" "<<it1->adj_remark<< endl;
ofs << it1->adj_id << " " << it1->adj_node << " " << it1->adj_cost<<" " << it1->adj_remark << endl;
}
}
}
};
int min = 9999,k=-1,i=0,j=-1;
int a[9][9] = {
0,10,max,max,max,11,max,max,max,
10,0,18,max,max,max,16, max, 12,
max,18,0,22,max,max,max,max, 8,
max,max,22,0,20,max,24, 16, 21,
max,max,max,20,0,26,max,7,max,
11,max,max,max,26,0,17, max, max,
max,16,max,24,max,17,0,19,max,
max,max,max,16,7,max,19,0,max,
max,12,8,21,max,max,max,max,0
};
graph G_graph;
//找到标记点的所有邻接点,并选出权值最小的节点返回
adj minfunc(vector<adj>mini) {
adj mini_node;
min = 999;
int key=-1;
for (vector<adj>::iterator it = mini.begin(); it != mini.end(); it++) {
if (min > it->adj_cost) {
min = it->adj_cost;
key = it->adj_node;
}
}
if (key != j) {
mini_node.adj_cost = min;
mini_node.adj_node = key;
mini_node.adj_remark = 1;
return mini_node;
}
}
//最小生成树
void min_tree() {
vector<adj>unremark_node;
adj unremark;
adj remark;
vector<adj>remark_node;
vector<node>::iterator it2;
map<int, node>::iterator it3;
G_graph.vertex.adj_cost= 0;
G_graph.vertex.adj_node = 0;
G_graph.vertex.adj_id = 0;
G_graph.vertex.adj_remark = 1;
remark_node.push_back(G_graph.vertex);
for (i = 1; i < 9; i++)
{
G_graph.vertex.adj_cost = 0;
G_graph.vertex.adj_node = 0;
G_graph.vertex.adj_id = i;
G_graph.vertex.adj_remark = 0;
remark_node.push_back(G_graph.vertex);
}
int min_node_id;
vector<adj>min_adj;
int num = 0, num1 = 1;
vector<adj>::iterator it1;
map<int, vector<adj>>::iterator it;
cout << "最小生成树为" << endl;
cout << '0' << "->";
while (num1 != 9) {
for (it = m_adj.begin(); it != m_adj.end() ; it++) {
if (remark_node[it->first].adj_remark != 1) {
continue;
}
second = it->second;
for (it1 = second.begin(); it1 != second.end(); it1++) {
if(remark_node[it1->adj_node].adj_remark != 1)
min_adj.push_back(*it1);
}
}
remark = minfunc(min_adj);//找到标记点的所有邻接点,并选出权值最小的节点返回
cout << remark.adj_node << "->";
remark_node[remark.adj_node] = remark;//把标记节点更新在标记容器中
num1++;
min_adj.clear();//每次找到权值最小的节点后,清空min_adj容器,便于下次使用
}
}
int main() {
G_graph.inputfile();
G_graph.out_adj();
min_tree();
system("pause");
}
2021-07-01
最新推荐文章于 2024-07-20 11:22:21 发布