伊吹萃香

23 篇文章 0 订阅

Description

  在幻想乡,伊吹萃香是能够控制物体密度的鬼王。因为能够控制密度,所以萃香能够制造白洞和黑洞,并可以随时改变它们。某一天萃香闲着无聊,在妖怪之山上设置了一些白洞或黑洞,由于引力的影响,给妖怪们带来了很大的麻烦。于是他们决定找出一条消耗体力最少的路,来方便进出。已知妖怪之山上有N个路口(编号1..N),每个路口都被萃香设置了一定质量白洞或者黑洞。原本在各个路口之间有M条单向路,走过每一条路需要消耗一定量的体力以及1个单位的时间。由于白洞和黑洞的存在,走过每条路需要消耗的体力也就产生了变化,假设一条道路两端路口黑白洞的质量差为delta:
  1. 从有白洞的路口走向有黑洞的路口,消耗的体力值减少delta,若该条路径消耗的体力值变为负数的话,取为0。
  2. 从有黑洞的路口走向有白洞的路口,消耗的体力值增加delta。
  3. 如果路口两端均为白洞或黑洞,消耗的体力值无变化。
  由于光是放置黑洞白洞不足以体现萃香的强大,所以她决定每过1个单位时间,就把所有路口的白洞改成黑洞,黑洞改成白洞。当然在走的过程中你可以选择在一个路口上停留1个单位的时间,如果当前路口为白洞,则不消耗体力,否则消耗s[i]的体力。现在请你计算从路口1走到路口N最小的体力消耗。保证一定存在道路从路口1到路口N。

Input

  第1行:2个正整数N, M
  第2行:N个整数,第i个数为0表示第i个路口开始时为白洞,1表示黑洞
  第3行:N个整数,第i个数表示第i个路口设置的白洞或黑洞的质量w[i]
  第4行:N个整数,第i个数表示在第i个路口停留消耗的体力s[i]
  第5..M+4行:每行3个整数,u, v, k,表示在没有影响的情况下,从路口u走到路口v需要消耗k的体力。

Output

  第1行:1个整数,表示消耗的最小体力

Sample Input

4 5

1 0 1 0

10 10 100 10

5 20 15 10

1 2 30

2 3 40

1 3 20

1 4 200

3 4 200

Sample Output

130

Data Constraint

Hint

【数据范围】
  对于30%的数据:1 <= N <= 100, 1 <= M <= 500
  对于60%的数据:1 <= N <= 1,000, 1 <= M <= 5,000
  对于100%的数据:1 <= N <= 5,000, 1 <= M <= 30,000
  其中20%的数据为1 <= N <= 3000的链
  1 <= u,v <= N, 1 <= k,w[i],s[i] <= 200
【样例说明】
  按照1 -> 3 -> 4的路线。

分析:设f[x,0]为偶数步到达x的最小代价,f[x,1]为奇数步到达x的最小代价,
b[x,0]为偶数步时的黑白情况,b[x,1]为奇数步时的黑白情况(初始为偶数)。当x为黑洞时,s[x]为停在x的代价,w[x]为x的质量。
当x–>y的代价为h时,可得
f[y,1]=max(f[y,1],f[x,0]+h) {b[x,0]=b[y,0]} (前进)
f[x,1]=max(f[x,1],f[x,0]+s[x]){b[x,0]=b[y,0]} (停止且。前面为黑洞+s[x],白洞不变)

f[y,0]=max(f[y,0],f[x,1]+h) {b[x,1]=b[y,1]} ( 前进 )
f[x,0]=max(f[x,0],f[x,1]+s[x]) {b[x,1]=b[y,1]}(停止且。前面为黑洞+s[x],白洞不变)

f[y,1]=max(f[y,1], f[x,0]+h+abs(w[x]-w[y]){b[x,0]=1 b[y,0]=0}(前进且前面为黑洞)
f[y,1]=max(f[y,1],f[x,0]+max(h-abs(w[i]-w[y]),0)){b[x,0]=0 b[y,0]=1}(前进且前面为白洞)
f[x,1]=max(f[x,1],f[x,0]){b[x,0]=0 b[y,0]=1}(停止且前方为白洞)
f[x,1]=max(f[x,1],f[x,0]+s[x]){b[x,0]=1 b[y,0]=0}(停止且前方为黑洞)

f[y,0]=max(f[y,0], f[x,1]+h+abs(w[x]-w[y]){b[x,1]=1 b[y,1]=0}(前进且前面为黑洞)
f[y,0]=max(f[y,0],f[x,1]+max(h-abs(w[i]-w[y]),0)){b[x,1]=0 b[y,1]=1}(前进且前面为白洞)
f[x,0]=max(f[x,0],f[x,1]){b[x,1]=0 b[y,1]=1}(停止且前方为白洞)
f[x,0]=max(f[x,0],f[x,1]+s[x]){b[x,1]=1 b[y,1]=0}(停止且前方为黑洞)

答案为max(f[n,0],f[n,1])

代码:

const
  MaxE=5001;
  MaxV=30001;

type
  rec=record
        x,y,h,next:longint;
      end;
var
  n,m,c,q,i,x,y:longint;
  g:array [1..Maxv] of rec;
  ls:array [1..Maxe] of longint;
  v,list,s,w:array [1..maxe] of longint;
  f:array [1..maxe,0..1] of longint;
  b:array [1..maxe,0..1] of 0..1;
function min(x,y:longint):longint;
 begin
  if x>y then exit(y)
         else exit(x);
 end;
procedure init;
 var i:longint;
begin
 readln(n,m);
for i:=1 to n do
 begin
  read(b[i,0]);
  b[i,1]:=1-b[i,0];
 end;
for i:=1 to n do
 read(w[i]);
for i:=1 to n do
 read(s[i]);
for i:=1 to m do
 begin
  with g[i] do
   begin
    read(x,y,h);
    next:=ls[x];
    ls[x]:=i;
   end;
 end;
end;

function max(x,y:longint):longint;
 begin
  if x>y then exit(x)
         else exit(y);
 end;

procedure spfa;
var
  head,tail,t,i,j,hehe:longint;
  flag,flag1:boolean;
begin
  head:=0; tail:=1;
  list[1]:=1;
  for i:=1 to n do
   for j:=0 to 1 do
    f[i,j]:=maxlongint div 3;
  f[1,0]:=0;
  v[1]:=1;
  while head<>tail do
    begin
      head:=head mod maxe+1;
      t:=ls[list[head]];
      while t>0 do
        with g[t] do
          begin
            flag:=true;
            flag1:=true;
            if (b[x,0]=b[y,0]) then
             begin
              if f[y,1]>f[x,0]+h then
               begin
                flag:=false;
                f[y,1]:=f[x,0]+h;
               end;
              if (b[x,0]=1) and (f[x,1]>f[x,0]+s[x]) then
               begin
                flag1:=false;
                f[x,1]:=f[x,0]+s[x];
               end;
              if (b[x,0]=0) and (f[x,1]>f[x,0]) then
               begin
                flag1:=false;
                f[x,1]:=f[x,0];
               end;
             end;

            if (b[x,1]=b[y,1]) then
             begin
               if f[y,0]>f[x,1]+h then
               begin
                flag:=false;
                f[y,0]:=f[x,1]+h;
               end;
              if (b[x,1]=1) and (f[x,0]>f[x,1]+s[x]) then
               begin
                flag1:=false;
                f[x,0]:=f[x,1]+s[x];
               end;
              if (b[x,1]=0) and (f[x,0]>f[x,1]) then
               begin
                flag1:=false;
                f[x,0]:=f[x,1];
               end;
             end;

            if (b[x,0]+b[y,0]=1) then
             begin
              hehe:=abs(w[x]-w[y]);
              if (b[x,0]=1) and (f[y,1]>f[x,0]+h+hehe) then
               begin
                flag:=false;
                f[y,1]:=f[x,0]+h+hehe;
               end;
              if (b[x,0]=0) and (f[y,1]>f[x,0]+max(h-hehe,0)) then
               begin
                flag:=false;
                f[y,1]:=f[x,0]+max(h-hehe,0);
               end;
              if (b[x,0]=0) and (f[x,1]>f[x,0]) then
               begin
                flag1:=false;
                f[x,1]:=f[x,0];
               end;
              if (b[x,0]=1) and (f[x,1]>f[x,0]+s[x]) then
               begin
                flag1:=false;
                f[x,1]:=f[x,0]+s[x];
               end;
             end;

             if (b[x,1]+b[y,1]=1) then
             begin
              hehe:=abs(w[x]-w[y]);
              if (b[x,1]=1) and (f[y,0]>f[x,1]+h+hehe) then
               begin
                flag:=false;
                f[y,0]:=f[x,1]+h+hehe;
               end;
              if (b[x,1]=0) and (f[y,0]>f[x,1]+max(h-hehe,0)) then
               begin
                flag:=false;
                f[y,0]:=f[x,1]+max(h-hehe,0);
               end;
              if (b[x,1]=0) and (f[x,0]>f[x,1]) then
               begin
                flag1:=false;
                f[x,0]:=f[x,1];
               end;
              if (b[x,1]=1) and (f[x,0]>f[x,1]+s[x]) then
               begin
                flag1:=false;
                f[x,0]:=f[x,1]+s[x];
               end;
             end;

            if flag=false then
             begin
              if v[y]=0 then
                  begin
                    v[y]:=1;
                    tail:=tail mod maxe+1;
                    list[tail]:=y;
                  end;
             end;
            v[list[head]]:=0;
            if flag1=false then
             begin
              v[x]:=1;
              tail:=tail mod maxe+1;
              list[tail]:=x;
             end;
            t:=next;
          end;
    end;
end;

begin
  init;
  spfa;
  if f[n,0]<f[n,1] then write(f[n,0])
                   else write(f[n,1]);
end.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值