0pt求助,悬赏两个关注
查看原帖
0pt求助,悬赏两个关注
481527
AC_CSP楼主2023/1/27 21:54
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
const int M=1e6+7;
struct edge{
	int nxt,v,w;
}e[M<<1];
int h[N],cnt;
inline void add_edge(int u,int v,int w){
	e[++cnt].nxt=h[u],e[cnt].v=v,e[cnt].w=w;
	h[u]=cnt;
}
int n,m,high[N];
long long ans;
struct node{
	int u,v,w;
}a[M<<1];
bool vis[N];
int tot;
int fa[N];
int p;
inline int find(int x){
	if(x!=fa[x]) return fa[x]=find(fa[x]);
	return x;
}
inline void dfs(int u){
	for(int i=h[u];i;i=e[i].nxt){
		int v=e[i].v,w=e[i].w;
		a[++tot].u=u,a[tot].v=v,a[tot].w=w;
		if(!vis[v]) vis[v]=1,p++,dfs(v);
	}
}
bool cmp(node x,node y){
	if(h[x.v]==h[y.v]) return x.w<y.w;
	return h[x.v]>h[y.v];
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&high[i]);
	for(int i=1;i<=m;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		if(high[u]>=high[v]) add_edge(u,v,w);
		if(high[u]<=high[v]) add_edge(v,u,w);
	}
	dfs(1);
	sort(a+1,a+tot+1,cmp);
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1;i<=tot;i++){
		int _=find(a[i].u),__=find(a[i].v);
		if(_!=__) fa[__]=_,ans+=a[i].w;
	}
	printf("%d %lld\n",p+1,ans);
	return 0;
}
2023/1/27 21:54
加载中...