Pascal 奖金 题目
发布网友
发布时间:2022-05-19 15:15
我来回答
共2个回答
热心网友
时间:2023-10-13 17:44
program Poor_Xed;
var
a, b : array of longint;
x : array of record
above : array of longint;
below, money : longint
end;
n, m, i, count : longint;
procere recur(k : longint);
var
i : longint;
begin
dec(count);
with x[k] do
for i:=1 to above[0] do begin
if money >= x[above[i]].money then
x[above[i]].money := money + 1;
dec(x[above[i]].below);
if x[above[i]].below = 0 then
recur(above[i])
end
end;
begin
readln(n, m);
setlength(x, n+1);
setlength(a, m+1);
setlength(b, m+1);
for i:=1 to m do
readln(a[i], b[i]);
for i:=1 to n do
with x[i] do begin
setlength(above, 1);
above[0] := 0;
end;
for i:=1 to m do
with x[b[i]] do begin
inc(above[0]);
if above[0] >= length(above) then
setlength(above, length(above) * 2);
above[above[0]] := a[i]
end;
for i:=1 to n do
x[i].below := 0;
for i:=1 to m do
inc(x[a[i]].below);
for i:=1 to n do
x[i].money := 100;
count := n;
for i:=1 to n do
if x[i].below = 0 then
recur(i);
if count = 0 then begin
for i:=1 to n do {begin
writeln(x[i].money);}
count := count + x[i].money;
{end;
writeln;}
writeln(count)
end else
writeln('Poor Xed')
end.
热心网友
时间:2023-10-13 17:45
拓扑排序改编的一道题
program topsort;
var
g:array[1..10000,1..10000]of 0..1;//储存图
use:array[1..10000]of boolean;//储存节点是否被访问过 如果被访问过则为True 否则为False
zhan:array[1..10000]of longint;
x,y,i,j,total,price,n,m,top:longint;
alluse:boolean;//判断节点是否全被访问过
function rdw0(k:longint):boolean;
var
i:longint;
begin
rdw0:=true;
for i:=1 to n do
if g[i,k]=1 then
begin
rdw0:=false;
break;
end;
end; //判断是否入度为0
procere cut(k:longint);
var
i:longint;
begin
for i:=1 to n do g[k,i]:=0;
end;//从图中把这个节点删除
BEGIN
fillchar(g,sizeof(g),0);
read(n,m);
for i:=1 to m do
begin
read(x,y);
g[y,x]:=1;
end;
fillchar(use,sizeof(use),false);
price:=100; total:=0;//读入及初始化
repeat
top:=0;
alluse:=true;
for j:=1 to n do
if not(use[j]) then
begin
alluse:=false;
break;
end;//判断节点是否全被访问过
for j:=1 to n do
if rdw0(j)and(not use[j]) then
begin
total:=total+price;
use[j]:=true;
inc(top);
zhan[top]:=j;
end;//找出入度为0的节点 进栈
for j:=1 to top do cut(zhan[j]);//删除栈中的节点
inc(price);
until alluse;
writeln(total);
END.