主题:我关于复赛的试题的解答(帮我看看)
shisutianxia
[专家分:630] 发布于 2007-11-22 13:56:00
提高组最后一题--树网的核
可惜的是我调不到使N>100 以上,希望大家能修改修改,帮忙
program core;
type ss=array[0..100]of 0..100;
var a:array[1..100,1..100]of integer;
v:array[1..100]of 0..1;
rec,daan:ss;
prec:array[1..100]of ss;
m,o,b,c,d,kw,t,n,s,i,j:integer;zhi,p,q:longint;
procedure search(k:longint);
var l,i,j:integer;
begin
for i:=1 to n do
if (v[i]=0)and(a[i,rec[t]]<>0) then begin
v[i]:=1;inc(t);rec[t]:=i;k:=k+a[i,rec[t-1]];
if k>kw then begin
for j:=1 to t do
daan[j]:=rec[j];
daan[0]:=t;kw:=k;
end;
search(k);
end;
v[t]:=0;
t:=t-1;
end;
function count(d,b:integer):longint;
var k,i:integer;
begin
k:=0;
for i:=d+1 to b do
k:=k+a[prec[o][i-1],prec[o][i]];
count:=k;
end;
begin
readln(n,s);
for i:=1 to 30 do
for j:=1 to 30 do
begin
a[i,j]:=0;
prec[i][j]:=0;;
end;
for i:=1 to n-1 do
begin
readln(b,c,d);
a[b,c]:=d;a[c,b]:=d;
end;
fillchar(v,sizeof(v),0);
fillchar(rec,sizeof(rec),0);
fillchar(daan,sizeof(daan),0);
p:=0;o:=0;
for i:=1 to n do
begin
v[i]:=1;t:=1;
rec[t]:=i;
kw:=0;
search(kw);
if kw>p then begin
p:=kw;o:=1;
for j:=0 to daan[0] do
prec[o][j]:=daan[j];
end;
if kw=p then begin
inc(o);
for j:=0 to daan[0] do
prec[o][j]:=daan[j];
end;
v[i]:=0;
end;
for i:=1 to o do
begin
for j:=1 to prec[o][0] do
write(prec[o][j]);
writeln;
end;
q:=maxint;
for i:=1 to o do
for j:=1 to prec[i][0] do
for m:=j to prec[i][0] do
begin
if count(j,m)>s then break;
if count(1,j)>count(m,prec[i][0]) then
zhi:=count(1,j)
else zhi:=count(m,prec[i][0]);
if zhi<q then q:=zhi;
end;
writeln(q);
end.
最后更新于:2007-11-24 16:52:00
回复列表 (共10个回复)
沙发
迷路的天使 [专家分:1340] 发布于 2007-11-22 18:05:00
我比你还菜`~~~~~
板凳
FancyMouse [专家分:13680] 发布于 2007-11-23 22:02:00
此题偶比赛的时候平方级算法,ac。赛后得出线性算法。
3 楼
shisutianxia [专家分:630] 发布于 2007-11-24 16:42:00
如果能说清楚些就更好!!!谢谢
4 楼
shisutianxia [专家分:630] 发布于 2007-11-24 16:43:00
上面算法还是有些问题,我要修改一下,好了,少了一个v[i]:=0;
5 楼
shisutianxia [专家分:630] 发布于 2007-11-24 16:56:00
当然,上面的方法会漏掉很多情况,我改-----------
6 楼
小田甜 [专家分:3910] 发布于 2007-11-25 00:06:00
我只求最大边
不到10分钟,30分到手……
很划算的……哈哈!
7 楼
shisutianxia [专家分:630] 发布于 2007-11-25 08:56:00
不是吧,最大边可能有多条呀!!!!!!!!你的程序给我看看,谢谢
8 楼
shisutianxia [专家分:630] 发布于 2007-11-25 09:34:00
第一题count.pas,冒泡
program count;
type ss=record
num:longint;
tot:longint;
end;
var rec:array[1..1000]of ss;
n,t,i,now:longint;
procedure sort;
var temp,i,j:longint;flag:boolean;
begin
i:=1;
repeat
flag:=true;
for j:=1 to t-i do
if (rec[j].tot<rec[j+1].tot) then begin
flag:=false;
temp:=rec[j].tot;
rec[j].tot:=rec[j+1].tot;
rec[j+1].tot:=temp;
temp:=rec[j].num;
rec[j].num:=rec[j+1].num;
rec[j+1].num:=temp;
end;
until flag;
end;
procedure panduan;
var i:longint;p:boolean;
begin
p:=true;
for i:=1 to t do
if now=rec[i].num then begin
inc(rec[i].tot);
p:=false;
break;
end;
if p=true then begin
inc(t);
inc(rec[t].tot);
rec[t].num:=now;
end;
end;
begin
readln(n);
t:=0;
for i:=1 to n do
begin
readln(now);
panduan;
end;
sort;
for i:=1 to t do
writeln(rec[i].num:10,rec[i].tot:10);
end.
9 楼
shisutianxia [专家分:630] 发布于 2007-11-25 10:39:00
第二题 EXPAND.APS
program expand;
var i,j,re,p1,p2,p3:integer;
st:string;ch:char;
procedure shuchu(a,b:char);
var i:char;j:integer;
begin
if p1=3 then
for i:=succ(a) to pred(b) do
for j:=1 to p2 do
write('*')
else begin
if p3=1then for i:=succ(a) to pred(b) do
for j:=1 to p2 do
write(i);
if p3=2 then for i:=pred(b) downto succ(a) do
for j:=1 to p2 do
write(i);
end;
end;
begin
readln(p1,p2,p3);
readln(st);
for i:=1 to length(st) do
begin
if st[i]<>'-'then write(st[i])
else begin
if st[i-1]>=st[i+1]then write('-')
else
if st[i-1] in['0'..'9'] then shuchu(st[i-1],st[i+1])
else
begin
if p1=2 then shuchu(chr(ord(st[i-1])+ord('A')-ord('a')),chr(ord(st[i+1])+ord('A')-ord('a')));
if p1=2 then shuchu(st[i-1],st[i+1]);
end;
end;
end;
end.
10 楼
123456789789456123 [专家分:0] 发布于 2009-04-09 21:53:00
第二题
program jkl;
var i,j,re,p1,p2,p3:integer;
st:string;
ch:char;
procedure shuchu(a,b:char);
var i:char;j:integer;
begin
if p1=3 then
for i:=succ(a) to pred(b) do
for j:=1 to p2 do
write('*')
else begin
if p3=1then for i:=succ(a) to pred(b) do
for j:=1 to p2 do
write(i);
if p3=2 then for i:=pred(b) downto succ(a) do
for j:=1 to p2 do
write(i);
end;
end;
我来回复