社区讨论

最小生成树蒟蒻求助

P1194买礼物参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mi7yjv5r
此快照首次捕获于
2025/11/21 05:43
4 个月前
此快照最后确认于
2025/11/21 05:43
4 个月前
查看原帖
这道题为什么会错555,思路是正确的呀,套一个模板不就O了=,但是

0分

还有RE,靠,上代码
CPP
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

const int MAXN = 5000 + 10;

int n,A,k,cnt,tot,ans;
int f[MAXN];
struct Line{
	int x,y;
	int num;
}line[MAXN];

inline int read(){
    int f = 1, x = 0;
    char c = getchar();

    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }

    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }

    return f * x;
}

void Add(int a,int b,int ttt){
	line[++cnt].x = a;
	line[cnt].y = b;
	line[cnt].num = cnt;
}

void Doing(){
	for(int i = 1;i <= n; i++)f[i] = i;
	return;
}

bool cmp(Line a,Line b){
	return a.num < b.num;
}

int find(int x){
	if(f[x] == x)return x;
	return f[x] = find(f[x]);
}

void unionn(int x,int y){
	int nx = find(x);
	int ny = find(y);
	if(nx != ny)f[nx] = ny;
}

void Kruskal(){
	for(int i = 1;i <= cnt; i++){
		int nx = find(line[i].x);
		int ny = find(line[i].y);
		if(nx == ny)continue;
		unionn(nx,ny);
		ans += line[i].num;
	}
}

int main(){
	A = read(),n = read();
	for(int i = 1;i <= n; i++){
		for(int j = 1;j <= n; j++){
			k = read();
			if(j < i && k != 0)Add(i,j,k);
		} 
		Add(0,i,A);
	}
	
	Doing();
	sort(line + 1,line + 1 + n,cmp);
	Kruskal();
	
	cout<<ans;
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...