南梁刚学 OI 114514s,求助,玄 1 关
查看原帖
南梁刚学 OI 114514s,求助,玄 1 关
764773
AstaVenti_楼主2024/11/30 14:44
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
ll n,m,ans=0,fa[100005],h[1000006],ltk,jd=0,q[1000006],cnt=0,ql=0,qr=0;
bool vis[1000006];
vector<pll>g[1000006];
struct edge{
	ll u,v,w;
}e[1000006];
bool cmp(edge a,edge b){
	if(h[a.v]!=h[b.v])return h[a.v]>h[b.v];
	else return a.w<b.w;
}
int find(int x){
	return x==fa[x]?x:fa[x]=find(fa[x]);
}
void bfs(){
    q[qr++]=1;vis[0]=1;
    while(ql<qr) {
        int now=q[ql++];
        for(auto i:g[now]){
        	e[cnt].u=now;e[cnt].v=i.first;e[cnt++].w=i.second;
            if(!vis[i.first])vis[i.first]=1,jd++,q[qr++]=i.first;
		}
    }
}
void K(){
	sort(e,e+m,cmp);
	for(int i=0;i<m;i++){
		ll u=find(e[i].u),v=find(e[i].v);
		if(u!=v){
			fa[u]=v,ans+=e[i].w,ltk--;
		}
		if(ltk==1)break;
	}
}

int main(){ 
	cin>>n>>m;
	ltk=n;
	for(ll i=0;i<n;i++)fa[i]=i;
	for(ll i=0;i<n;i++)cin>>h[i];
	for(ll i=0,u,v,w;i<m;i++){
		cin>>u>>v>>w;
		if(h[u]>=h[v]) g[u].push_back({v,w});
		if(h[u]<=h[v]) g[v].push_back({u,w});
	}
	bfs();
	K();
	cout<<jd+1<<" "<<ans;
}

2024/11/30 14:44
加载中...