Brine
A certain person trying to make his way in the universe.
Index[0]
Index[1]
Disjoint Set
vector<int> master(vertexCount);
int query(int i) {
if (master[i] == i) return i;
return query(master[i]);
}
int query(int i) {
return (master[i] == i ? i : query(master[i]));
}
int query(int i) {
if (master[i] == i) return i;
master[i] = query(master[i]);
return master[i];
}
struct DisjointSet {
vector<int> master, depth;
disjointSet(int vertexCount) {
master.resize(vertexCount);
depth.resize(vertexCount);
for (int i = 0; i < vertexCount; i++) {
master[i] = i;
depth[i] = 1;
}
}
int query(int i) {
if (master[i] == i) return i;
return master[i] = query(master[i]);
}
void merge(int a, int b) {
a = query(a);
b = query(b);
if (a == b) return;
if (depth[a] < depth[b]) swap(a, b);
master[b] = a;
depth[a] += depth[b];
}
}; // 記得分號
Spanning Tree
typedef pair<int, int> pii;
typedef pair<int, pii> pi_ii;
struct DisjointSet {
vector<int> master;
DisjointSet(int vertexCount) {
master.resize(vertexCount);
iota(master.begin(), master.end(), 0);
}
int query(int a) {
if (a == master[a]) return a;
return master[a] = query(master[a]);
}
bool connected(int a, int b) {
return query(a) == query(b);
}
void merge(int a, int b) {
master[query(a)] = master[b];
}
};
int kruskal(vector<pi_ii>& minEdge, DisjointSet& ds) {
int cost = 0;
for (auto& [w, uv]: minEdge) {
auto& [u, v] = uv;
if (ds.connected(u, v)) continue;
ds.merge(u, v);
cost += w;
}
return cost;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int vertexCount, edgeCount;
cin >> vertexCount >> edgeCount;
DisjointSet ds(vertexCount);
vector<pi_ii> minEdge(edgeCount);
for (auto& [w, uv]: minEdge) {
auto& [u, v] = uv;
cin >> u >> v >> w;
}
sort(minEdge.begin(), minEdge.end());
cout << kruskal(minEdge, ds) << '\n';
}
typedef pair<int, int> pii;
int prim(vector< vector<pii> >& graph) {
priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<int> currentMinCost(graph.size(), INT32_MAX);
vector<bool> inside(graph.size(), false);
int sum = 0;
pq.push({0, 0});
currentMinCost[0] = 0;
while (!pq.empty()) {
auto [w, u] = pq.top();
pq.pop();
if (inside[u]) continue;
inside[u] = true;
sum += w;
for (auto& [w, v]: graph[u]) {
if (!inside[v] && currentMinCost[v] > w) {
currentMinCost[v] = w;
pq.push({w, v});
}
}
}
return sum;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int vertexCount, edgeCount;
cin >> vertexCount >> edgeCount;
vector< vector<pii> > graph(vertexCount);
int u, v, w;
for (int i = 0; i < edgeCount; i++) {
cin >> u >> v >> w;
graph[u].push_back({w, v});
graph[v].push_back({w, u});
}
cout << prim(graph) << '\n';
}
Shortest Path
vector< vector<int> > d(vC, vector<int>(vC, 1e9 + 225));
for (int i = 0; i < vC; i++) d[i][i] = 0;
int u, v, w;
for (int i = 0; i < eC; i++) {
cin >> u >> v >> w;
d[u][v] = min(d[u][v], w);
}
for (int k = 0; k < vC; k++) {
for (int i = 0; i < vC; i++) {
for (int j = 0; j < vC; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
struct Edge {
int u, v, w;
};
vector<int> Bellman_Ford(int vertexCount, int source, vector<Edge>& edge) {
vector<int> distance(vertexCount, INT32_MAX);
distance[source] = 0;
for (int i = 1; i < vertexCount; i++) {
for (auto& [u, v, w]: edge) {
distance[u] = min(distance[u], distance[v] + w);
distance[v] = min(distance[v], distance[u] + w);
}
}
return distance;
}
typedef pair<int, int> pii;
vector<int> SPFA(vector< vector<pii>& graph, int source) {
vector<int> distance(graph.size(), INT32_MAX);
distance[source] = 0;
queue<int> q;
vector<bool> inQueue(graph.size(), 0);
q.push(source);
while (!q.empty()) {
auto v = q.front();
q.pop();
inQueue[v] = 0;
for (auto& [u, w]: graph[v]) {
distance[u] = min(distance[u], distance[v] + w);
if (inQueue[u]) continue;
q.push(u);
inQueue[u] = 1;
}
}
return distance;
}
typedef pair<int, int> pii;
vector<int> Dijkstra(vector< vector<pii> >& graph, int source) {
vector<int> distance(graph.size(), INT32_MAX);
distance[source] = 0;
vector<bool> visited(graph.size(), 0);
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push({0, source});
while (!pq.empty()) {
auto [w, u] = pq.top();
pq.pop();
if (visited[u]) continue;
visited[u] = 1;
for (auto& [w, v]: graph[u]) {
if (distance[v] <= distance[u] + w) continue;
distance[v] = distance[u] + w;
pq.push({distance[v], v});
}
}
return distance;
}
By Brine
圖論[1]