我嘞个最大生成树
2024-10-27 15:16:19
发布于:广东
7阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <memory.h>
#define int long long
using namespace std;
int a[2005][2005];
int dis[2005];
bool vis[2005];
int n, m, x, y, len, add = 3e9, mod = 1e9 + 7;
int peruyimu(){
for(int i = 2; i <= n; i++){
dis[i] = a[1][i];
}
int ct = 0;
vis[1] = 1;
for(int i = 2; i <= n; i++){
int mx = 0, idx = -1;
for(int j = 2; j <= n; j++){
if(!vis[j] && mx < dis[j]){
mx = dis[j], idx = j;
}
}
if(idx == -1) return -1;
vis[idx] = 1;
ct += dis[idx];
for(int j = 2; j <= n; j++) dis[j] = max(dis[j], a[idx][j]);
}
return ct;
}
int read(){
char c = getchar();
int x = 0, f = 1;
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * f;
}
signed main(){
memset(a, 63, sizeof(a));
n = read();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++) a[i][j] = read();
}
cout << peruyimu();
return 0;
}
这里空空如也
有帮助,赞一个