讨论 / 大牛看看为什么超时
ttdd8 2011-02-09 03:42:00
点我顶贴 收藏 删除
我用的纯粹的prim为什么超时?还有一个点没有过,能不能帮我看看,或者告诉我一下数据

program ttdd8;

var cost:array[1..1000,1..1000] of longint;

mincost:array[1..1000] of longint;

sum,min,k,i,j,m,n,x,y,z:longint;

begin

readln(n,m);

fillchar(cost,sizeof(cost),0);

for i:=1 to m do

begin

readln(x,y,z);

if cost[x,y]=0 then

begin

cost[x,y]:=z;

cost[y,x]:=z;

end

else if z<cost[x,y] then

begin

cost[x,y]:=z;

cost[y,x]:=z;

end;

end;

for i:=1 to n do

for j:=1 to n do

if (i<>j) and (cost[i,j]=0) then cost[i,j]:=maxint;

for i:=1 to n do

mincost[i]:=cost[1,i];

for i:=2 to n do

begin

min:=maxint;

for j:=1 to n do

if (mincost[j]<min) and (mincost[j]<>0) then

begin

min:=mincost[j];

k:=j;

end;

inc(sum,min);

mincost[k]:=0;

for j:=1 to n do

if (mincost[j]>cost[k,j]) and (mincost[j]<>0) then

mincost[j]:=cost[k,j];

end;

writeln(sum);

end.

通过本测试点|有效耗时188:ms

测试结果2: 选手程序运行超过时限

测试结果3: 测试结果错误.错误结果为:59271

正确结果应为:139949

测试结果4: 通过本测试点|有效耗时719:ms

测试结果5: 通过本测试点|有效耗时485:ms

测试结果6: 通过本测试点|有效耗时734:ms

测试结果7: 通过本测试点|有效耗时250:ms

测试结果8: 通过本测试点|有效耗时344:ms

测试结果9: 通过本测试点|有效耗时188:ms

测试结果10: 通过本测试点|有效耗时63:ms

#1 DarkMaster@2008-08-27 01:36:00
回复 删除
用堆优化看看?
#2 xiaokeke@2008-08-27 01:39:00
回复 删除
我用的纯粹的prim ac的好好的啊
#3 xiaokeke@2008-08-27 01:43:00
回复 删除
把存储改成 integer试试

只把sum设成longint就够了

内存也影响速度的

#4 DarkMaster@2008-08-27 01:45:00
回复 删除
对哦,他还开了1000*1000的数组饿....
#5 Leaspos@2011-02-09 03:42:00
回复 删除
回复 地毯xiaokeke 的帖子

不过原来好像看到过说longint比integer更快吧?

查看更多回复
提交回复