-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwdigraph.h
41 lines (33 loc) · 950 Bytes
/
wdigraph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef _WEIGHTED_GRAPH_H_
#define _WEIGHTED_GRAPH_H_
#include <unordered_map>
#include "digraph.h"
using namespace std;
/*
Represents a weighted graph using
an adjacency list representation.
Vertex identifiers are integers.
Weights are of type long long.
*/
class WDigraph : public Digraph {
public:
// No constructor or destructor are necessary this time.
// A new instance will be an empty graph with no nodes.
// returns the cost/weight of an edge
// if it does not exist, returns error
long long getCost(int u, int v) const {
// uses .at because there is no const operator[]
// for unordered maps
return cost.at(u).at(v);
}
// adds a weighted edge
// if the edge already existed, does nothing
void addEdge(int u, int v, long long w){
// use Digraph's addEdge method
Digraph::addEdge(u, v);
cost[u][v] = w;
}
private:
unordered_map<int, unordered_map<int, long long>> cost;
};
#endif