写了很久发现对不了,然后根据讨论中的dalao指出此题应有重边
所以将标程魔改了一波
var
n,m,i,j,k,c,d,e:longint;
a,b:array [0..10000] of longint;
f:array [0..2300,0..2300] of longint;
dis:array [0..10000] of longint;
min,sum:longint;
used:array [0..10000] of boolean;
begin
readln(n);
readln(m);
fillchar(f,sizeof(f),$7f);
for i:=1 to m do
begin
readln(c,d,e);
if (f[c,d]<>f[0,0]) then
if (f[c,d]<e) then continue;
f[c,d]:=e;
f[d,c]:=e;
end;
fillchar(dis,sizeof(dis),$7f);
dis[0]:=0;
sum:=0;
for i:=0 to n do
begin
min:=maxlongint;
for j:=0 to n do
if not used[j] and (dis[j]<min) then
begin
k:=j;
min:=dis[j];
end;
used[k]:=true;
sum:=sum+dis[k];
for j:=1 to n do
if not used[j] and (f[k,j]<dis[j]) then
dis[j]:=f[k,j];
end;
writeln(sum);
end.
将其改为可以判断重边,然后就错了
下为我的程序
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#define INF 0x7f7f7f7f
#define maxn 2300001
#define max(x,y) x>y?x:y
using namespace std;
int ans=0;
int f[13000];
struct arr
{
int x,y,w;
}a[maxn];
inline int read()
{
int x=0,p=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch == '-') p=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
return x*p;
}
int cmp(arr a,arr b)
{
return a.w<b.w;
}
int find(int x)
{
if (!f[x]) return x;
f[x]=find(f[x]);
return f[x];
}
int insert(int x,int y)
{
if (find(x)!=find(y))
{
f[find(x)]=find(y);
return 1;
}
return 0;
}
int main()
{
int n=read(),m=read();
for (int i=1;i<=m;i++)
{
a[i].x=read(),a[i].y=read(),a[i].w=read();
}
sort(a+1,a+m+1,cmp);
for (int i=1;i<=m;i++)
{
if (find(a[i].x)!=find(a[i].y))
{
ans+=a[i].w;
insert(a[i].x,a[i].y);
}
}
printf("%d\n",ans);
}
@wzf2000 @kkksc03