社区讨论

84pts TLE求调

P3366【模板】最小生成树参与者 2已保存回复 10

讨论操作

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

当前回复
10 条
当前快照
1 份
快照标识符
@mhksvq9l
此快照首次捕获于
2025/11/05 00:45
4 个月前
此快照最后确认于
2025/11/08 07:46
3 个月前
查看原帖
我用PrimTLE了,是要用Kruskal吗?
红名关,橙以下互关。
不要直接给代码!!!

代码

CPP
#include<bits/stdc++.h>
using namespace std;
struct node {
	int x;
	int y;

};
struct cmp {
	bool operator()(node a, node b) {
		return a.x>b.x;
	}
};
int n, m;
bool v[5005];
priority_queue<node,vector<node>,cmp> q;
vector<node> a[5005];
int prim(int ans, int x, int jian) {
    v[x]=1;
	if (ans == n) return jian;
	for (int i = 0; i < a[x].size(); i++) {
		q.push(a[x][i]);
	}
	while (1) {
		node now = q.top();
		q.pop();
		if (!v[now.y]) {
			return prim(ans + 1, now.y, jian + now.x);
		}
	}
	return -1;
}

int main() {
	//memset(a, 127, sizeof(a));
    ios_base::sync_with_stdio(false);
	cin>>n>>m;
    int shou=0;
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin>>x>>y>>z;
        shou=x;
        a[x].push_back({z, y});
        a[y].push_back({z, x});
        
	}
	long long annnns=prim(1,1,0);
	if(annnns==-1) cout<<"orz";
	else cout<<annnns;
	return 0;
}

回复

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

正在加载回复...