尼玛啊,C++的哈希真是难用到爆,我还非常执拗的不想用boost,结果发现也没啥差,functional的hash函数效果一样。
代码没有验证,但是问题不大
#include<iostream>
#include<vector>
#include<list>
#include<unordered_map>
#include<unordered_set>
#include <string>
#include<functional>
#include<string.h>
using namespace std;
class Edge;
class Node ///<节点
{
public:
int value;//权值
int in;//入度
int out;//出度
list<Edge> edge;//出的边
list<Node> nextnode;//出的边连的节点
Node(int valuee)
{
value = valuee;
in = 0;
out = 0;
}
};
class Edge ///<边
{
public:
int weight;//权重
Node *from;//有向图的起点
Node *to;//有向图的终点
int num;
Edge(int weightt, Node *fromm, Node *too)
{
weight = weightt;
from = fromm;
to = too;
num = -1;
}
};
struct func //hash 函数
{
size_t operator()(const Edge a) const
{
return ((hash<int>()(a.weight)
^ (hash<Node*>()(a.from) << 1)) >> 1)
^ (hash<Node*>()(a.to) << 1);
}
};
struct cmp_fun //比较函数 ==
{
bool operator()(const Edge &a, const Edge &b) const
{
return memcmp(&a, &b, sizeof(a)) == 0 ? true:false;
}
};
class Graph//完整的图结构
{
public:
unordered_map<int, Node>node;
unordered_set<Edge, func,cmp_fun>edge;
};
Graph graph;
void ReadGraph(vector<vector<int>>a)//读图程序
{
int n = a.size();
for (int i = 0; i<n; ++i)
{
int weight = a[i][0];
int from = a[i][1];
int to = a[i][2];
///<下面两行代码过后不管from和to是否出现过就都有了
if (graph.node.find(from) == graph.node.end())//如果from点从没在哈希中出现过就加入
graph.node.insert(pair<int, Node>(from, Node(from)));
if (graph.node.find(to) == graph.node.end())//如果to点从没在哈希中出现过就加入
graph.node.insert(pair<int, Node>(to, Node(to)));
Node* p_fromNode = &graph.node.find(from)->second;//在哈希表中提出from点
Node* p_toNode = &graph.node.find(to)->second;//提出to点
p_fromNode->nextnode.push_back(*p_toNode);//from节点的next节点是to
++p_fromNode->out;//from的出度++
++p_toNode->in;//to节点的入度++
Edge newedge = Edge(weight, p_fromNode, p_toNode);//建立一条边
p_fromNode->edge.push_back(newedge);//把这条边加入到from点的边集中
graph.edge.insert(newedge);//把这条边加入到图中
}
}
int main(void)
{
vector<vector<int>>a;
for (int i = 0; i<8; ++i)
{
vector<int>temp;
for (int j = 0; j<3; ++j)
{
int xx;
cin >> xx;
temp.push_back(xx);
}
a.push_back(temp);
}
ReadGraph(a);
return 0;
}