zoukankan      html  css  js  c++  java
  • Mobile Service (Standard IO)

    题意/Description:

            一个公司有三个移动服务员。如果某个地方有一个请求,某个员工必须赶到那个地方去(那个地方没有其他员工),某一时刻只有一个员工能移动。被请求后,他才能移动,不允许在同样的位置出现两个员工。从p到q移动一个员工,需要花费c(p,q)。这个函数没有必要对称,但是c(p,p)=0。公司必须满足所有的请求。目标是最小化公司花费。

     

    读入/Input

            第一行有两个整数L,N(3<=L<=200, 1<=N<=1000)。L是位置数;N是请求数。每个位置从1到L编号。下L行每行包含L个非负整数。第i+1行的第j个数表示c(i,j) ,并且它小于2000。最后一行包含N个数,是请求列表。一开始三个服务员分别在位置1,2,3。

     

    输出/Output

            一个数M,表示最小服务花费。

     

    题解/solution

           这是一个dp,这一眼就可以看出。
           但是暴力的dp是会爆空间和时间的。
           所以要改一改。
           首先,要用滚动数组,因为我们的空间不足。
           具体方程见程序。
           还要手动把第一个请求设为初值。

     

    代码/Code

    var
      n,m,t,min,x:longint;
      b:array [0..4001] of longint;
      a:array [0..201,0..201] of longint;
      f:array [0..1,0..201,0..201] of longint;
    function minn(o,p:longint):longint;
    begin
      if o<p then exit(o);
      exit(p);
    end;
    
    procedure init;
    var
      i,j:longint;
    begin
      fillchar(f,sizeof(f),$7f div 3);
      readln(n,m);
      for i:=1 to n do
        for j:=1 to n do
          read(a[i,j]);
      for i:=1 to m do
        read(b[i]);
      t:=f[0,0,0];
    end;
    
    procedure main;
    var
      i,j,k:longint;
    begin
      x:=0;
      f[0,1,2]:=a[3,b[1]]; f[0,2,1]:=f[0,1,2];
      f[0,1,3]:=a[2,b[1]]; f[0,3,1]:=f[0,1,3];
      f[0,2,3]:=a[1,b[1]]; f[0,3,2]:=f[0,2,3];
      for k:=2 to m do
        begin
          x:=x xor 1;
          for i:=1 to n do
            for j:=1 to n do
              f[x,i,j]:=t;
          for i:=1 to n do
            for j:=1 to n do
              if (i<>j) and (i<>b[k-1]) and (j<>b[k-1]) then
                begin
                  f[x,i,j]:=minn(f[x,i,j],f[x xor 1,i,j]+a[b[k-1],b[k]]);
                  f[x,j,i]:=f[x,i,j];
                  f[x,b[k-1],j]:=minn(f[x,b[k-1],j],f[x xor 1,i,j]+a[i,b[k]]);
                  f[x,j,b[k-1]]:=f[x,b[k-1],j];
                  f[x,b[k-1],i]:=minn(f[x,b[k-1],i],f[x xor 1,i,j]+a[j,b[k]]);
                  f[x,i,b[k-1]]:=f[x,b[k-1],i];
                end;
        end;
    end;
    
    procedure print;
    var
      i,j:longint;
    begin
      min:=maxlongint;
      for i:=1 to n do
        for j:=1 to n do
          if min>f[x,i,j] then min:=f[x,i,j];
      write(min);
    end;
    
    begin
      init;
      main;
      print;
    end.



  • 相关阅读:
    RabbitMQ实战(文摘)
    dex2jar 和 jd-gui 的安装与使用(转)
    asp.net core跨平台开发从入门到实战文摘
    hashCode() 和equals() 区别和作用(转)
    B+树和LSM比较(转)
    C#并发集合(转)
    文档docsify
    如何熟悉一个系统?(内含知识大图)
    新浪微博应对弹性扩容的架构演进
    iftop非交互式监控流量来源和去向
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319666.html
Copyright © 2011-2022 走看看