图就是一堆点和边,但是可以用不同的方式来存储和表示。
比如这一种
#include <vector>
#include <stdlib.h>
struct Edge {
int v;
int w;
Edge* next;
Edge():v(-1), next(NULL){}
};
struct Graph {
int V;
int E;
std::vector<Edge*> adj;
std::vector<Edge> edge_pool;
Edge* pool_tail;
Graph(int v, int e): V(v), E(e) {
adj.resize(V);
edge_pool.resize(E);
pool_tail = &edge_pool[0];
}
void add_edge(int u, int v, int w) {
pool_tail->v = v;
pool_tail->w = w;
pool_tail->next = adj[u];
adj[u] = pool_tail;
++pool_tail;
}
};
/*
for (Edge* e = graph->adj[i];e;e = e->next)
*/
比如这一种
#include <vector>
struct Edge {
int src;
int dest;
int weight;
};
struct Graph {
int V;
int E;
std::vector<Edge> edge;
Graph(int v, int e):V(v), E(e) {
edge.resize(E);
}
};