图的组成元素:
顶点
边
建立顶点类,成员包包括:入度,出度,数值,以此顶点为出发点的边(放入链表中) 当前结点直接相连的下一个结点(放入链表中)
class Node
{
public:
int val;
int in;
int out;
list<Node*>next;
list<Edge*>edges;
Node(int x) :val(x),in(0),out(0){}
};
边类的构成:边的权值 出发顶点 末尾结点
class Edge
{
public:
int weight;
Node* from;
Node* to;
Edge(int x, Node* from, Node* to) :weight(x),from(from),to(to){}
};
图类:
class Graph
{
public:
unordered_map<int, Node*>nodes;
unordered_set<Edge*>edges;
};
Graph Create(vector<vector<int>>v)
{
Graph graph;
for (int i = 0; i < v.size(); i++)
{
int weight = v[i][0];
int from = v[i][1];
int to = v[i][2];
if (graph.nodes.find[from] = graph.nodes.end())
graph.nodes[from] = new Node(from);
if (graph.nodes.find[to] = graph.nodes.end())
graph.nodes[to] = new Node(to);
Node* fromnode = graph.nodes[from];
Node* tonode = graph.nodes[to];
Edge* edge = new Edge(weight, fromnode, tonode);
fromnode->next
}
}