```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define INF 0x3f3f3f3f
#define MAXN 5001
#define MAXM 200001
using namespace std;
int n,m;
int dis[MAXN];
bool used[MAXN];
int nxt[MAXM],value[MAXM],endd[MAXM];
int head[MAXN];
int cnt=0,sum=0;
void add_edge(int a,int b,int c)
{
cnt++;
value[cnt]=c,endd[cnt]=b;
nxt[cnt]=head[a];
head[a]=cnt;
}
int main()
{
memset(head,0,sizeof(head));
memset(dis,-1,sizeof(dis));
memset(used,0,sizeof(used));
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
dis[1]=0;
for (int i=1;i<=n;i++)
{
int mind=INF,u;
for (int j=1;j<=n;j++)
{
if (!used[j])
{
if (dis[j]!=-1&&dis[j]<mind)
{
mind=dis[j];
u=j;
}
}
}
used[u]=1;
//printf("******\n");
//printf("u = %d\n",u);
for (int j=head[u];j!=0;j=nxt[j])
{
int v=endd[j],c=value[j];
/*printf("******\n");
printf("%d -> %d : %d\n",u,v,c);
printf("pre value : %d\n",dis[v]);*/
if (!used[v])
{
if (dis[v]==-1||dis[v]>c)
dis[v]=c;
}
//printf("now value : %d\n",dis[v]);
}
}
for (int i=1;i<=n;i++)
{
if (dis[i]==-1)
{
printf("orz\n");
return 0;
}
sum+=dis[i];
}
printf("%d\n",sum);
return 0;
}
```
by KonnyakuZeri @ 2017-08-08 16:44:31
上上层忘了打cpp,格式乱得一塌糊涂,看上一层吧这个吧
by KonnyakuZeri @ 2017-08-08 16:45:01
我是链式前向星+普里姆,wa了三个点。看题解我怀疑是没判断重边和自环(蒟蒻只是怀疑)
by 石榴 @ 2017-08-23 16:29:32