旅游(travel)

题目大意:
    给出一个图,求在花费不超过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.


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值