C++供应链管理模块的图数据结构描述

M在某些供应链管理模块,我们使用邻接表来表示图,其中每个顶点表示一个节点(例如仓库、生产厂家、分销商等),每条边表示节点之间的关系(例如运输路径、供应关系等)。

```cpp

#include
#include
#include

// 顶点表示节点struct Vertex {
    std::string name;
    // 可以添加其他属性,如库存量、生产能力等};

// 边表示节点之间的关
struct Edge {
    int weight;  // 边的权重,表示运输距离、供应关系
    // 可以添加其他属性,如运输方式、运输成本等};

// 使用邻接表表示
class Graph {
private:
    std::unordered_map>> adjList;

public:
    void addVertex(const Vertex& v) {
        adjList[v] = std::vector>();
    }

    void addEdge(const Vertex& v1, const Vertex& v2, const Edge& e) {
        adjList[v1].push_back(std::make_pair(v2, e));
        // 如果是无向图,还需要添加 v2 到 v1 的边    }

    void printGraph() {
        for (const auto& entry : adjList) {
            std::cout << "Vertex " << entry.first.name << ":\n";
            for (const auto& neighbor : entry.second) {
                std::cout << "  -> " << neighbor.first.name << " (weight: " << neighbor.second.weight << ")\n";
            }
        }
    }
};

测试:

int main() {
    Graph supplyChainGraph;

    Vertex warehouse1 = {"Warehouse 1"};
    Vertex warehouse2 = {"Warehouse 2"};
    Vertex manufacturer1 = {"Manufacturer 1"};
    Vertex distributor1 = {"Distributor 1"};

    supplyChainGraph.addVertex(warehouse1);
    supplyChainGraph.addVertex(warehouse2);
    supplyChainGraph.addVertex(manufacturer1);
    supplyChainGraph.addVertex(distributor1);

    Edge edge1 = {100};  // 例如,表示运输距离    Edge edge2 = {150};

    supplyChainGraph.addEdge(warehouse1, manufacturer1, edge1);
    supplyChainGraph.addEdge(manufacturer1, warehouse2, edge2);
    supplyChainGraph.addEdge(manufacturer1, distributor1, edge1);

    supplyChainGraph.printGraph();

    return 0;
}
```

定义`Vertex`和`Edge`结构来表示顶点和边,然后使用`Graph`类来构建图。

你可能感兴趣的:(C++开发大全,经验分享,c++,数据结构,图搜索算法,qt)