题目大意:
给出一个图,求在花费不超过money的情况下最多获得的价值。
算法:图论(SPFA)
分析:用最裸的SPFA做,不用做松弛操作,因为找价值最大的只要满足花费不超过money就行,所以每拓展一个结点就打擂台。
program travel;
const
maxn=30000;
maxm=60000;
type
atp=record
y,next,cost,value:longint;
end;
atpp=record
c,v:longint;
end;
var
n,m,money,tot,head,tail:longint;
b:array [0..maxn] of boolean;
que,first:array [0..maxn] of longint;
v:array [0..maxn] of atpp;
map:array [0..maxm] of atp;
procedure init;
var
i,x,y,cost,value:longint;
begin
tot:=0;
readln(n,m,money);
for i:=1 to m do
begin
readln(x,y,cost,value);
inc(tot);
map[tot].y:=y;
map[tot].cost:=cost;
map[tot].value:=value;
map[tot].next:=first[x];
first[x]:=tot;
inc(tot);
map[tot].y:=x;
map[tot].cost:=cost;
map[tot].value:=value;
map[tot].next:=first[y];
first[y]:=tot;
end;
end;
procedure main;
var
i,j,t,max:longint;
begin
max:=0;
for i:=1 to n do
begin
fillchar(b,sizeof(b),false);
head:=0;
tail:=1;
v[i].c:=0;
v[i].v:=0;
b[i]:=true;
que[1]:=i;
while head<tail do
begin
inc(head);
t:=first[que[head]];
while t>0 do
begin
if (not b[map[t].y]) and (v[que[head]].c+map[t].cost<=money) then
begin
inc(tail);
que[tail]:=map[t].y;
b[map[t].y]:=true;
v[map[t].y].c:=v[que[head]].c+map[t].cost;
v[map[t].y].v:=v[que[head]].v+map[t].value;
if v[map[t].y].v>max then max:=v[map[t].y].v;
end;
t:=map[t].next;
end;
end;
end;
writeln(max);
end;
begin
assign(input,'travel.in'); reset(input);
assign(output,'travel.out'); rewrite(output);
init;
main;
close(input); close(output);
end.
1272

被折叠的 条评论
为什么被折叠?



