主题:[讨论]奖金--一道模拟题,有点难度哦!!
游侠UFO
[专家分:1200] 发布于 2005-10-27 13:43:00
[题目描述]
QB清北公司为了民主,搞了一个座谈会,每个员工对自己的奖金发表意见.每位参加会谈的员工只能说:"我认为员工a的奖金应该比b高!"公司总经理Mr.H决定找出一种奖金方案,满足各位代表的意见,且同时使得总奖金数最少.每位员工奖金最少为100元.
[输入]
第一行两个整数n,m,表示员工总数和代表数;
以下m行,每行2个整数a,b,表示某个代表认为第a号员工奖金应该比第b好员工高.
[输出]
若无法找到合法方案,则输出"Poor Xed"; 否则输出一个数表示最少总奖金.
[样例输入]
2 1
1 2
[样例输出]
201
[数据范围]
80%的数据满足n<=1000, m<=2000;
100%的数据满足n<=10000. m<=20000.
回复列表 (共7个回复)
沙发
游侠UFO [专家分:1200] 发布于 2005-10-27 13:47:00
下面是我自己写的代码,有问题,具体的请看注释:
program reward;
type
graphtype=array[1..10000,1..10000] of 0..1;
stucktype=record
data:array[1..10000] of integer;
top:integer;
end;
treenode=record
data:integer;
lch:integer;
rch:integer;
end;
treetype=array[1..10000] of treenode;
var
delnode:treetype;
graph:graphtype;
stuck:stucktype;
node:array[1..10000] of integer;
m,n,c1,co,money,a,b,i,ans:integer;
{这个程序能通过FP的编译,但是无法运行.如果把上面的10000改成100就可以运行了,但是就不符合题目所给出的范围了}
function tryindegree(g:graphtype; x:integer):integer;
var
i,ans:integer;
begin
ans:=0;
for i:=1 to n do
if g[i,x]=1 then ans:=ans+1;
tryindegree:=ans;
end;
procedure insert(var tree:treetype; x,root:integer; var count:integer);
begin
if count=0 then
begin
count:=count+1;
tree[count].data:=x;
end
else
if x<=tree[root].data then
begin
if tree[root].lch=0 then
begin
count:=count+1;
tree[root].lch:=count;
tree[count].data:=x;
end
else insert(tree,x,tree[root].lch,count);
end
else
if tree[root].rch=0 then
begin
count:=count+1;
tree[root].rch:=count;
tree[count].data:=x;
end
else insert(tree,x,tree[root].rch,count);
end;
function isin(tree:treetype; x,root:integer):boolean;
var
ans:boolean;
begin
if root<>0 then
begin
ans:=false;
if x=tree[root].data then ans:=true
else
begin
if x<tree[root].data then ans:=isin(tree,x,tree[root].lch);
if x>tree[root].data then ans:=isin(tree,x,tree[root].rch);
end;
isin:=ans;
end;
end;
procedure push(var s:stucktype; x:integer);
begin
s.top:=s.top+1;
s.data[s.top]:=x;
end;
function pop(var s:stucktype):integer;
begin
pop:=s.data[s.top];
s.top:=s.top-1;
end;
procedure topsort(g:graphtype; var s:stucktype; var delnode:treetype);
var
b:boolean;
i,y:integer;
begin
b:=false;
for i:=1 to n do
if (tryindegree(g,i)=0) and (not isin(delnode,i,1)) then
begin
push(s,i);
b:=true;
end;
while s.top>0 do
begin
y:=pop(s);
co:=co+1;
node[y]:=money;
for i:=1 to n do g[y,i]:=0;
insert(delnode,y,1,c1);
end;
money:=money+1;
if b then topsort(g,s,delnode);
end;
{main program}
begin
{init}
money:=100;
{input}
readln(n,m);
for i:=1 to m do
begin
readln(a,b);
graph[b,a]:=1;
end;
{work}
topsort(graph,stuck,delnode);
if co=n then
for i:=1 to co do ans:=ans+node[i]
else write('Poor Xed');
{output}
if co=n then writeln(ans);
end.
板凳
游侠UFO [专家分:1200] 发布于 2005-10-27 17:47:00
今天在老师的提示下换了一种写法,应该能行了,至少我测试时是这样.
下面是代码:
program reward_2;
type
nodetype=array[1..10000] of integer;
graphrecord=record
a:integer;
b:integer;
end;
graphtype=array[1..20000] of graphrecord;
stucktype=record
data:array[1..10000] of integer;
top:integer;
end;
var
node:nodetype;
graph:graphtype;
stuck:stucktype;
m,n,money,i,j,co,ans:integer;
procedure tryindegree(graph:graphtype; var node:nodetype);
var
i:integer;
begin
for i:=1 to m do node[graph[i].a]:=node[graph[i].a]+1;
end;
procedure push(var stuck:stucktype; x:integer);
begin
stuck.top:=stuck.top+1;
stuck.data[stuck.top]:=x;
end;
function pop(var stuck:stucktype):integer;
begin
pop:=stuck.data[stuck.top];
stuck.top:=stuck.top-1;
end;
procedure topsort(graph:graphtype; var node:nodetype; var stuck:stucktype);
var
b:boolean;
i,y:integer;
begin
b:=false;
for i:=1 to n do
if node[i]=0 then
begin
push(stuck,i);
b:=true;
end;
while stuck.top>0 do
begin
ans:=ans+money;
y:=pop(stuck);
co:=co+1;
node[y]:=-1;
for i:=1 to m do
if y=graph[i].b then node[graph[i].a]:=node[graph[i].a]-1;
end;
money:=money+1;
if b then topsort(graph,node,stuck);
end;
{main program}
begin
{init}
money:=100;
{input}
readln(n,m);
for i:=1 to m do readln(graph[i].a,graph[i].b);
{work}
tryindegree(graph,node);
topsort(graph,node,stuck);
{output}
if co=n then writeln(ans)
else writeln('Poor Xed');
end.
3 楼
zyx326 [专家分:0] 发布于 2005-10-27 19:13:00
我是个菜鸟怎么办啊?
就要复赛了,,我初赛才40多分,,
4 楼
游侠UFO [专家分:1200] 发布于 2005-10-28 13:47:00
第二种方法我试了一下比较大的数据,输出的答案是正确的,但程序会不正常结束
但如果把graphtype的长度改为10000好像就不会有问题了
5 楼
小虾虾 [专家分:300] 发布于 2005-11-16 17:03:00
你的思路是什么?
6 楼
游侠UFO [专家分:1200] 发布于 2005-11-17 17:54:00
拓扑排序
7 楼
Sincera [专家分:10] 发布于 2005-11-17 22:08:00
拓扑是对的;
用搜索的话~~~~汗
我来回复