过样例 WA 0pts,实在调不动了
查看原帖
过样例 WA 0pts,实在调不动了
550821
No_Rest楼主2024/8/26 15:06

rt

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pll;
const ll maxn = 1e5 + 5;
const ll maxm = 2e6 + 5;
struct st{
	ll u, v, w;
}ed[maxm];
ll n, m, h[maxn], vis[maxn], fa[maxn], ans, sum, tot;
vector <pll> e[maxn];
void dfs(ll x){
	vis[x] = 1, sum++;
	for(pll i : e[x]) 
		if(!vis[i.first]){
			++tot;
			ed[tot].u = x, ed[tot].v = i.first, ed[tot].w = i.second;
			dfs(i.first);
		}
}
bool cmp(st a1, st a2){ 
	if(h[a1.v] == h[a2.v]) return a1.w < a2.w;
	return h[a1.v] > h[a2.v];
}
ll find(ll x){ return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for(ll i = 1; i <= n; ++i) cin >> h[i], fa[i] = i;
	for(ll i = 1, u, v, w; i <= m; ++i){
		cin >> u >> v >> w;
		if(h[u] >= h[v]) e[u].push_back({v, w});
		if(h[u] <= h[v]) e[v].push_back({u, w});
	}
	dfs(1);
	cout << sum << ' ';
	sort(ed + 1, ed + tot + 1, cmp);
	for(ll i = 1, fx, fy; i <= tot; ++i){
		fx = find(ed[i].u), fy = find(ed[i].v);
		if(fx != fy){
			fa[fx] = fy, ans += ed[i].w, sum--;
			if(sum <= 1) break;
		}
	}
	cout << ans;
 	return 0;
} 
2024/8/26 15:06
加载中...