题目链接:HDU 1233 还是畅通工程
题意:对给出的图,求最小生成树的总长度。
代码:
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
const int N = 2e4 + 10;
struct Node {
int u, v, w;
Node() {}
Node(int a, int b, int c) {
u = a, v = b, w = c;
}
friend bool operator < (Node a, Node b) {
return a.w > b.w;
}
};
vector<Node> g[N];
int f[N];
int n, m;
int _find(int x) {
return f[x] = (f[x] == x ? x : _find(f[x]));
}
int kruskal() {
priority_queue<Node> q;
for (int i = 1; i <= n; i++)
for (int j = 0; j < g[i].size(); j++)
q.push(g[i][j]);
int res = 0, ct = 0;
while (!q.empty()) {
if (ct == n - 1)
break;
Node ft = q.top();
q.pop();
int fu = _find(ft.u);
int fv = _find(ft.v);
if (fu != fv) {
res += ft.w;
f[fu] = fv;
ct++;
}
}
if (ct == n - 1)
return res;
return -1;
}
int main() {
while (scanf("%d", &n) != EOF && n) {
for (int i = 0; i <= n; i++)
g[i].clear();
m = (n * (n - 1)) / 2;
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].push_back(Node(u, v, w));
}
for (int i = 1; i <= n; i++)
f[i] = i;
printf("%d\n", kruskal());
}
return 0;
}
题目链接:POJ 2485 Highways
题意:求最小生成树中的最大边。
代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int N = 5e2 + 10;
const int INF = 0x3f3f3f3f;
struct Node {
int u, v, w;
Node() {}
Node(int a, int b, int c) {
u = a, v = b, w = c;
}
friend bool operator < (Node a, Node b) {
return a.w > b.w;
}
};
int edge[N][N];
int f[N];
bool vis[N];
int n;
int _find(int x) {
return f[x] = (f[x] == x ? x : _find(f[x]));
}
int kruskal() {
int res = 0;
priority_queue<Node> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (edge[i][j])
q.push(Node(i, j, edge[i][j]));
while (!q.empty()) {
Node ft = q.top();
q.pop();
int fu = _find(ft.u);
int fv = _find(ft.v);
if (fu != fv) {
f[fu] = fv;
res = max(res, ft.w);
}
}
return res;
}
int prim() {
int res = 0;
priority_queue<Node> q;
vis[1] = true;
for (int i = 1; i <= n; i++)
if (edge[1][i])
q.push(Node(1, i, edge[1][i]));
while (!q.empty()) {
Node ft = q.top();
q.pop();
if (!vis[ft.v]) {
res = max(res, ft.w);
vis[ft.v] = true;
for (int i = 1; i <= n; i++)
if (edge[ft.v][i])
q.push(Node(ft.v, i, edge[ft.v][i]));
}
}
return res;
}
int main() {
int t_case;
scanf("%d", &t_case);
for (int i_case = 1; i_case <= t_case; i_case++) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &edge[i][j]);
for (int i = 1; i <= n; i++)
f[i] = i;
memset(vis, false, sizeof(vis));
// printf("%d\n", kruskal());
printf("%d\n", prim());
}
return 0;
}
题目链接:POJ 1861 Network
题意:求最小生成树中的最大边,并输出生成树。(题目要求不一定是最小生成树,但最小生成树一定可以满足条件,所以样例给出的并不是最小生成树)
代码:
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
const int N = 2e4 + 10;
struct Node {
int u, v, w;
Node() {}
Node(int a, int b, int c) {
u = a, v = b, w = c;
}
friend bool operator < (Node a, Node b) {
return a.w > b.w;
}
};
vector<Node> g[N], edge;
int f[N];
int n, m;
int _find(int x) {
return f[x] = (f[x] == x ? x : _find(f[x]));
}
int kruskal() {
priority_queue<Node> q;
for (int i = 1; i <= n; i++)
for (int j = 0; j < g[i].size(); j++)
q.push(g[i][j]);
int res = 0, ct = 0;
while (!q.empty()) {
if (ct == n - 1)
break;
Node ft = q.top();
q.pop();
int fu = _find(ft.u);
int fv = _find(ft.v);
if (fu != fv) {
res = max(res, ft.w);
f[fu] = fv;
edge.push_back(ft);
ct++;
}
}
if (ct == n - 1)
return res;
return -1;
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
edge.clear();
for (int i = 0; i <= n; i++)
g[i].clear();
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].push_back(Node(u, v, w));
}
for (int i = 1; i <= n; i++)
f[i] = i;
int ans = kruskal();
int size = edge.size();
printf("%d\n%d\n", ans, size);
for (int i = 0; i < edge.size(); i++)
printf("%d %d\n", edge[i].u, edge[i].v);
}
return 0;
}
题目链接:POJ 2395 Out of Hay
题意:求最小生成树的最大边。
代码:
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
const int N = 2e4 + 10;
struct Node {
int u, v, w;
Node() {}
Node(int a, int b, int c) {
u = a, v = b, w = c;
}
friend bool operator < (Node a, Node b) {
return a.w > b.w;
}
};
vector<Node> g[N];
int f[N];
int n, m;
int _find(int x) {
return f[x] = (f[x] == x ? x : _find(f[x]));
}
int kruskal() {
priority_queue<Node> q;
for (int i = 1; i <= n; i++)
for (int j = 0; j < g[i].size(); j++)
q.push(g[i][j]);
int res = 0, ct = 0;
while (!q.empty()) {
if (ct == n - 1)
break;
Node ft = q.top();
q.pop();
int fu = _find(ft.u);
int fv = _find(ft.v);
if (fu != fv) {
res = max(res, ft.w);
f[fu] = fv;
ct++;
}
}
if (ct == n - 1)
return res;
return -1;
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 0; i <= n; i++)
g[i].clear();
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].push_back(Node(u, v, w));
}
for (int i = 1; i <= n; i++)
f[i] = i;
printf("%d\n", kruskal());
}
return 0;
}