解题报告 大富翁

No.3 大富翁

   Maxingc买来的大富翁游戏规则有些不一样。

这是一个N*N 的方形网格,设其左上角为起点◎,坐标为(1,1),X 轴向右为正,Y

轴向下为正,每个方格边长为1,如图所示。一辆汽车从起点◎出发驶向右下角终点▲,其

坐标为(N,N)。在若干个网格交叉点处,设置了油库,可供汽车在行驶途中加油。汽车在

行驶过程中应遵守如下规则:

(1)汽车只能沿网格边行驶,装满油后能行驶K 条网格边。出发时汽车已装满油,在起

点与终点处不设油库。

(2)汽车经过一条网格边时,若其X 坐标或Y 坐标减小,则应付费用B,否则免付费用。

(3)汽车在行驶过程中遇油库则应加满油并付加油费用A。

(4)在需要时可在网格点处增设油库,并付增设油库费用C(不含加油费用A)。

(5)(1)~(4)中的各数N、K、A、B、C均为正整数,且满足约束:2 <=n<=100,2<=k<=10;

 

 

 Maxingc让窄森和暖熊在1s的时间内求出从起点到终点最少的花费。否则就不让他们今天晚上睡觉。Hzoiers,帮帮这些孩子吧!

     输入格式:

   文件的第一行是N,K,A,B,C的值。第二行起是一个N*N 的0-1 方阵,每行N 个值,至N+1 行结束。方阵的第i 行第j 列处的值为1 表示在网格交叉点(i,j)处设置了一个油库,为0 时表示未设油库。各行相邻两个数以空格分隔。

  输出格式:

     最小费用。

  输入样例:

   9 3 2 3 6

0 0 0 0 1 0 0 0 0

0 0 0 1 0 1 1 0 0

1 0 1 0 0 0 0 1 0

0 0 0 0 0 1 0 0 1

1 0 0 1 0 0 1 0 0

0 1 0 0 0 0 0 1 0

0 0 0 0 1 0 0 0 1

1 0 0 1 0 0 0 1 0

0 1 0 0 0 0 0 0 0

  输出样例:

     12

 

二维 SPFA 。深搜的光荣 TLE 掉。。。。。。。

解析神马的,就不说了,水水的二维 SPFA 。。。。。。

好吧,我承认,像这种矩阵路径的,还加上改变原图的,我一看到它就想深搜。。。。。。

图论啊图论。。。。。

 

不过注意一点,因为他没走一个格都要费油,所以他一定不会走回头路,所以增设的加油站只能用一次,所以不用改变原图,所以不用考虑存储问题,所以 BFS 的优化(二维SPFA)轻松水过。。。。。

 

代码 dsqwwe

program dsqwwe;

  const

     dx:array[1..4] of longint=(0,0,1,-1);

     dy:array[1..4] of longint=(1,-1,0,0);

     w:array[1..4] of longint=(0,1,0,1);

  type

    tt=record

     x,y,s:longint;

    end;

  var

    d:array[1..200000] of tt;

    v:array[1..100,1..100,0..10] of boolean;

    dis:array[1..100,1..100,0..10] of longint;

    closed,open,n,k,a,b,c,x,y,s,i,xx,yy,ss,temp,ans,j:longint;

map:array[1..100,1..100] of longint;

 

  begin

    assign(input,'car.in');

    reset(input);

    assign(output,'car.out');

    rewrite(output);

 

    readln(n,k,a,b,c);

    for i:=1 to n do

     for j:=1 to n do

      read(map[i,j]);

 

    closed:=0; open:=1;

    d[1].x:=1; d[1].y:=1; d[1].s:=k;

    filldword(dis,sizeof(dis)>>2,999999);

    dis[1,1,k]:=0;

    v[1,1,k]:=true;  

    while closed<>open do

     begin

       inc(closed);

       if closed>n*n*k then closed:=1;

       x:=d[closed].x;

       y:=d[closed].y;

       s:=d[closed].s;

       if s<>0 then

       for i:=1 to 4 do

        begin

          xx:=x+dx[i];

          yy:=y+dy[i];

          if (xx>0) and (xx<=n) and (yy>0) and (yy<=n) then

           begin

             temp:=w[i]*b;

             if map[xx,yy]=1 then

              begin

                ss:=k;

                temp:=temp+a;

              end

             else begin

                    ss:=s-1;

                    if (ss=0) and ((xx<>n) or (yy<>n)) then

                     begin

                       ss:=k;

                       temp:=temp+c+a;

                     end;

                  end;

             if dis[xx,yy,ss]>dis[x,y,s]+temp then

              begin

                dis[xx,yy,ss]:=dis[x,y,s]+temp;

                if not v[xx,yy,ss] then

                 begin

                   v[xx,yy,ss]:=true;

                   inc(open);

                   if open>n*n*k then open:=1;

                   d[open].x:=xx;

                   d[open].y:=yy;

                   d[open].s:=ss;

                 end;

              end;

            end;

        end;

       v[x,y,s]:=false;

     end;

    ans:=maxlongint;

    for i:=0 to k do

     if dis[n,n,i]<ans then ans:=dis[n,n,i];

    writeln(ans);

 

    close(input);

    close(output);

  end.

 

原文地址:https://www.cnblogs.com/SueMiller/p/2234717.html