用数学语言讲,设G为图,对图的每一条边e来说,都对应于一个实数W(e)(可以通俗的理解为边的“长度”,只是在数学定义中图的权可以为负数),我们把W(e)称为e的“权”。把这样的图G称为“加权图”。
加权无向图数据结构使用邻接表来存储
源代码示例:
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
class Edge
{
public:
Edge(size_t _v, size_t _w, double _weight) : v(_v), w(_w), weight(_weight) {}
double getWeight() const { return weight; }
size_t either() const { return v; }
size_t other(size_t v) const
{
if (v == this->v)
return this->w;
else if (v == this->w)
return this->v;
else
{
cout << "error in Edge::other()" << endl;
exit(1);
}
}
void print() c