#include<iostream>
#include<vector>
#include<string>
#include<windows.h>
#define Inifinity 65535
class NetNode {
public:
friend class DV;
private:
std::vector<std::vector<int>>direction_vector;
struct info {
std::string node_name;
unsigned int node_id;
}node_info;
void InitNetNode(const int& node_number, const std::pair<std::string, int>& info) {
node_info.node_name = info.first;
node_info.node_id = info.second;
direction_vector.resize(node_number);
for (int i = 0; i < node_number; ++i) {
direction_vector[i].resize(node_number);
if (i == node_info.node_id) {
std::cout << "Please input value:";
for (int j = 0; j < node_number; ++j) {
int value;
std::cin >> value;
direction_vector[i][j] = value;
}
}
else {
for (int j = 0; j < node_number; ++j) {
direction_vector[i][j] = Inifinity;
}
}
}
}
std::vector<int> return_self_value(const int& id) const {
std::vector<int> self_value;
for (auto& value : direction_vector[id])
self_value.push_back(value);
return self_value;
}
void BellmanFord_Function()
{
std::cout << "NetNode-" + node_info.node_name << " now is using bellmanford function to update!" << std::endl;
for (int i = 0; i < direction_vector.size(); ++i)
{
if (i == node_info.node_id)
continue;
direction_vector[node_info.node_id][i]
= get_min_value(i);
}
}
void show_value() const {
for (int i = 0; i < direction_vector.size(); ++i)
{
for (int j = 0; j < direction_vector.size(); ++j)
{
std::cout << direction_vector[i][j] << " ";
}
std::cout << std::endl;
}
}
int get_min_value(const int& destination)
{
int res = Inifinity;
for (int i = 0; i < direction_vector.size(); ++i)
{
if (i == node_info.node_id)
continue;
if ((direction_vector[node_info.node_id][i] + direction_vector[i][destination]) < res)
{
res = direction_vector[node_info.node_id][i] + direction_vector[i][destination];
}
}
return res;
}
};
class DV {
public:
explicit DV(const int& netnode_number)
:m_netnode_number(netnode_number)
{
InitNet();
}
void DV_algorithm() {
while (true)
{
bool flag = false;
for (int i = 0; i < m_netnode_number; ++i)
{
if (receive_message(i)) flag = true;
else flag = false;
Sleep(1000);
}
if (flag)
{
for (int i = 0; i < m_netnode_number; ++i)
{
m_netnode[i].BellmanFord_Function();
m_netnode[i].show_value();
Sleep(1000);
}
}
else break;
}
}
void show_net_value() const {
for (int i = 0; i < m_netnode_number; ++i) {
m_netnode[i].show_value();
std::cout << std::endl;
}
}
static int index;
private:
std::vector<NetNode>m_netnode;
int m_netnode_number;
private:
void InitNet()
{
for (int i = 0; i < m_netnode_number; ++i)
{
NetNode netnode;
std::cout << "Please give a name for your netnode:";
std::string name;
std::cin >> name;
netnode.InitNetNode(m_netnode_number, std::make_pair(name, DV::index));
m_netnode.push_back(netnode);
DV::index++;
}
}
bool receive_message(const int& id) {
bool flag = false;
std::cout << "NetNode-" + m_netnode[id].node_info.node_name << " now is receiving messages!" << std::endl;
for (int i = 0; i < m_netnode_number; ++i)
{
if (i == id)
continue;
int column = 0;
for (auto value : m_netnode[i].return_self_value(i)) {
if (m_netnode[id].direction_vector[i][column] == value)
{
column++;
continue;
}
m_netnode[id].direction_vector[i][column] = value;
column++;
if (!flag) flag = true;
}
}
m_netnode[id].show_value();
return flag;
}
};
int DV::index = 0;
void Test()
{
DV dv(3);
dv.DV_algorithm();
dv.show_net_value();
}
int main()
{
Test();
return 0;
}
测试用例: 运行结果:
|