题目: | 神经网络 | |
来源: | Noip 2003T1 | |
题目大意: | 从输入层开始,各节点按照传递公式,一层一层向下传递。输出输出层中信号大于零 的节点编号和信号大小。(节点编号由小到大)如果没有满足条件的编号则输出 NULL。 | |
数据范围: | 1≤n≤200 | |
样例: | 5 6 1 0 1 0 0 1 0 1 0 1 1 3 1 1 4 1 1 5 1 2 3 1 2 4 1 2 5 1 | 3 1 4 1 5 1 |
做题思路: | 广搜,注意C是>0是才向下传,输出时按从小到大。这次代码写的好猥琐。。。。 | |
知识点: | Bfs |
type
edge=record
y,next,w:longint;
end;
var
a:array[0..500]of edge;
d,first,c:array[0..500]of longint;
q:array[0..200000]of longint;
b:array[0..500]of boolean;
n,p,tot:longint;
pc:boolean;
procedure ad_edge(x,y,w:longint);
begin
inc(tot);
a[tot].y:=y;
a[tot].w:=w;
a[tot].next:=first[x];
first[x]:=tot;
end;
procedure init;
var
i,x,y,w,sum:longint;
begin
readln(n,p);
fori:=1 to n do
begin
readln(x,y);
ifx<>0 then
begin
c[i]:=x;{<输入点C>0>}
end
else
begin
c[i]:=-y; {<非输入点的C=0,而且后面要用到U,所以提前赋给他省得麻烦>}
end;
end;
tot:=0;
fori:=1 to p do
begin
readln(x,y,w);
ad_edge(x,y,w);
end;
end;
procedure bfs;
var
head,tail,t,i:longint;
begin
fillchar(b,sizeof(b),false);
head:=1;tail:=0;
fori:=1 to n do
ifc[i]>0 then
begin
inc(tail);
q[tail]:=i;
b[i]:=true;
end;
tot:=0;
whilehead<=tail do
begin
t:=first[q[head]];
ift>0 then{<有传出点的继续bfs,没有的记录下来等待输出>}
begin
ifc[q[head]]>0 then{<大于0的才传递这让我丢了一次分>}
while t>0 do
begin
c[a[t].y]:=c[a[t].y]+a[t].w*c[q[head]];
ifnot b[a[t].y] then
begin
inc(tail);
q[tail]:=a[t].y;
b[a[t].y]:=true;
end;
t:=a[t].next;
end;
end
else
begin
inc(tot);
d[tot]:=q[head];
end;
b[q[head]]:=false;
inc(head);
end;
end;
procedure qsort(l,r:longint);
var
i,j,k,t:longint;
begin
i:=l;j:=r;
k:=d[(l+r)shr 1];
repeat
whiled[i]<k do inc(i);
whiled[j]>k do dec(j);
ifi<=j then
begin
t:=d[i];d[i]:=d[j];d[j]:=t;
inc(i);dec(j);
end;
untili>j;
ifi<r then qsort(i,r);
ifj>l then qsort(l,j);
end;
procedure outit;
var
i,m:longint;
begin
qsort(1,tot);
pc:=false;
m:=-maxlongint;
fori:=1 to tot do
if c[d[i]]>0 then{<题目要求只输出大于0的>}
begin
pc:=true;
if(m<>d[i]) then{<bfs记录时猥琐了,所以这里只能继续猥琐——判重>}
begin
writeln(d[i],' ',c[d[i]]);
m:=d[i];
end;
end;
if notpc then writeln('NULL');
end;
begin
assign(input,'network.in');reset(input);
assign(output,'network.out');rewrite(output);
init;
bfs;
outit;
close(input);close(output);
end.
题目来源: http://yt.tyvj.cn:8080/Problem_Show.asp?id=1038