Files
Data-Structure/模板/其他/比较函数的模板6种.cpp
2025-12-16 20:36:27 +08:00

34 lines
778 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

1
auto cmp = [&](const Edge& a, const Edge& b) {
return a.cost > b.cost; // 小根堆cost 小的优先
};
priority_queue<Edge, vector<Edge>, decltype(cmp)> pq(cmp);
2
struct Compare {
bool operator()(const Edge& a, const Edge& b) const {
return a.cost > b.cost; // 小根堆
}
};
priority_queue<Edge, vector<Edge>, Compare> pq;
3
using F = function<bool(const Edge&, const Edge&)>;
priority_queue<Edge, vector<Edge>, F> pq(
[](const Edge& a, const Edge& b){
return a.cost > b.cost; // 小根堆
}
);
4
struct Edge{
int from, to, cost;
Edge(int f, int t, int c): from(f), to(t), cost(c) {}
bool operator<(const Edge& other) const {
return cost > other.cost; // 小根堆
}
//一定是<,不能是>
}