|
|
|
@ -47,44 +47,39 @@ $3≤n≤100$
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const int N = 110;
|
|
|
|
|
const int INF = 0x3f3f3f3f;
|
|
|
|
|
|
|
|
|
|
int n;
|
|
|
|
|
int g[N][N]; // 邻接矩阵,记录每两个点之间的距离
|
|
|
|
|
int dis[N]; // 每个点距离集合的最小长度
|
|
|
|
|
bool st[N]; // 是不是已经加入到集合中
|
|
|
|
|
int g[N][N];
|
|
|
|
|
int dis[N];
|
|
|
|
|
bool st[N];
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
int prim() {
|
|
|
|
|
// 初始化所有节点到集合的距离为正无穷
|
|
|
|
|
memset(dis, 0x3f, sizeof dis);
|
|
|
|
|
dis[1] = 0; // 1号节点到集合的距离为0
|
|
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
|
for (int i = 1; i <= n; i++) { // 迭代n次
|
|
|
|
|
for (int i = 0; i < n; i++) { // 迭代n次
|
|
|
|
|
int t = -1;
|
|
|
|
|
//(1)是不是第一次
|
|
|
|
|
//(2)如果不是第1次那么找出距离最近的那个点j
|
|
|
|
|
for (int j = 1; j <= n; j++)
|
|
|
|
|
if (!st[j] && (t == -1 || dis[t] > dis[j])) // 第一次就是猴子选大王,赶鸭子上架
|
|
|
|
|
t = j;
|
|
|
|
|
// 最小生成树的距离和增加dis[t]
|
|
|
|
|
res += dis[t];
|
|
|
|
|
// t节点入集合
|
|
|
|
|
if (!st[j] && (t == -1 || dis[t] > dis[j])) t = j;
|
|
|
|
|
if (i && dis[t] == INF) return INF; // 非连通图,没有最小生成树
|
|
|
|
|
if (i) res += dis[t];
|
|
|
|
|
for (int j = 1; j <= n; j++)
|
|
|
|
|
if (!st[j] && g[t][j] < dis[j]) {
|
|
|
|
|
dis[j] = g[t][j];
|
|
|
|
|
}
|
|
|
|
|
st[t] = true;
|
|
|
|
|
// 利用t,拉近其它节点长度
|
|
|
|
|
for (int j = 1; j <= n; j++) dis[j] = min(dis[j], g[t][j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
cin >> n;
|
|
|
|
|
// 完全图,每两个点之间都有距离,不用考虑无解情况
|
|
|
|
|
// 初始化
|
|
|
|
|
memset(g, 0x3f, sizeof g);
|
|
|
|
|
memset(dis, 0x3f, sizeof dis);
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= n; i++)
|
|
|
|
|
for (int j = 1; j <= n; j++)
|
|
|
|
|
cin >> g[i][j];
|
|
|
|
|
|
|
|
|
|
// 利用prim算法计算最小生成树
|
|
|
|
|
cin >> g[i][j]; // 有向图
|
|
|
|
|
cout << prim() << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -95,46 +90,57 @@ int main() {
|
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const int N = 110;
|
|
|
|
|
const int M = 10010;
|
|
|
|
|
const int N = 110, M = 10010;
|
|
|
|
|
const int INF = 0x3f3f3f3f;
|
|
|
|
|
int n, m; // n个节点,m条边
|
|
|
|
|
|
|
|
|
|
struct Node { //用结构体存储每条边
|
|
|
|
|
int f, t, w;
|
|
|
|
|
bool operator<(const Node &e) const {
|
|
|
|
|
return w < e.w;
|
|
|
|
|
// Kruskal用到的结构体
|
|
|
|
|
struct Node {
|
|
|
|
|
int a, b, c;
|
|
|
|
|
bool const operator<(const Node &t) const {
|
|
|
|
|
return c < t.c; // 边权小的在前
|
|
|
|
|
}
|
|
|
|
|
} edges[M];
|
|
|
|
|
} edge[M]; // 数组长度为是边数
|
|
|
|
|
|
|
|
|
|
// 并查集
|
|
|
|
|
int p[N];
|
|
|
|
|
|
|
|
|
|
int find(int x) { //并查集找根节点
|
|
|
|
|
int find(int x) {
|
|
|
|
|
if (p[x] != x) p[x] = find(p[x]);
|
|
|
|
|
return p[x];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int n, idx, ans;
|
|
|
|
|
int main() {
|
|
|
|
|
scanf("%d", &n);
|
|
|
|
|
int res; // 最小生成树的权值和
|
|
|
|
|
int cnt; // 最小生成树的结点数
|
|
|
|
|
|
|
|
|
|
// Kruskal算法
|
|
|
|
|
int kruskal() {
|
|
|
|
|
// 1、按边权由小到大排序
|
|
|
|
|
sort(edge, edge + m);
|
|
|
|
|
// 2、并查集初始化
|
|
|
|
|
for (int i = 1; i <= n; i++) p[i] = i;
|
|
|
|
|
// 3、迭代m次
|
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
|
|
|
int a = edge[i].a, b = edge[i].b, c = edge[i].c;
|
|
|
|
|
a = find(a), b = find(b);
|
|
|
|
|
if (a != b)
|
|
|
|
|
p[a] = b, res += c, cnt++; // cnt是指已经连接上边的数量
|
|
|
|
|
}
|
|
|
|
|
// 4、特判是不是不连通
|
|
|
|
|
if (cnt < n - 1) return INF;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//邻接矩阵
|
|
|
|
|
int main() {
|
|
|
|
|
cin >> n;
|
|
|
|
|
// 邻接矩阵
|
|
|
|
|
for (int i = 1; i <= n; i++)
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
int w;
|
|
|
|
|
scanf("%d", &w);
|
|
|
|
|
edges[idx++] = {i, j, w}; //加入当前的边
|
|
|
|
|
}
|
|
|
|
|
sort(edges, edges + idx); //对边权进行排序,注意这里不是优先队列,是谁小谁在前
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= n; i++) p[i] = i; //并查集初始化
|
|
|
|
|
for (int i = 1; i <= idx; i++) { //枚举每条边
|
|
|
|
|
int f = find(edges[i].f), t = find(edges[i].t);
|
|
|
|
|
if (f != t) { //当前两点不连通
|
|
|
|
|
ans += edges[i].w; //更新答案
|
|
|
|
|
p[f] = t; //让两点变连通
|
|
|
|
|
int c;
|
|
|
|
|
cin >> c;
|
|
|
|
|
edge[m++] = {i, j, c}; // 加入当前的边
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("%d", ans);
|
|
|
|
|
|
|
|
|
|
int t = kruskal();
|
|
|
|
|
cout << t << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|