@[AC_CSP](/user/481527) 改过了
您出现了两个问题
```cpp
#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(high[x.v]==high[y.v]) return x.w<y.w;//h和high弄混了吧
return high[x.v]>high[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);
}
p=1,vis[1]=1;//初始化有问题 之前的方式可能导致一个环重复经过点1使得答案大1
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,ans);
return 0;
}
```
by Killer_joke @ 2023-01-27 22:12:43
@[Killer_joke](/user/915814) 谢谢!已关注!
by AC_CSP @ 2023-01-27 22:33:47