zoukankan      html  css  js  c++  java
  • 线性DP SPOJ Mobile Service

    Mobile Service

    A company provides service for its partners that are located in different towns. The company has three mobile service staff employees. If a request occurs at some location, an employee of the service staff must move from his current location to the location of the request (if no employee is there) in order to satisfy the request. Only one employee can move at any moment. They can move only on request and are not allowed to be at the same location. Moving an employee from location p to location q incurs a given cost C(p,q). The cost function is not necessarily symmetric, but the cost of not moving is 0, i.e. C(p,p)=0. The company must satisfy the received requests in a strict first-come, first-serve basis. The goal is to minimize the total cost of serving a given sequence of requests.

    Task

    You are to write a program that decides which employee of the service staff is to move for each request such that the total cost of serving the given sequence of requests is as small as possible.

    Input

    The first line of input contains the number of test cases - nTest. Each test case contains:

    The first line of each test cases contains two integers, L and N. L (3 <= L <= 200) is the number of locations and N (1 <= N <= 1000) is the number of requests. Locations are identified by the integers from 1 to L. Each of the next L lines contains L non-negative integers. The jth number in the line i+1 is the cost C(i,j), and it is less than 2000.

    The last of each test cases contains N integers, the list of the requests. A request is identified by the identifier of the location where the request occurs. Initially, the three service staff employees are located at location 1, 2 and 3, respectively.

    Output

    For each test case write the minimal total cost in a separate line.

    Example

    Input:
    1
    5 9
    0 1 1 1 1
    1 0 2 3 2
    1 1 0 4 1
    2 1 5 0 1
    4 2 3 4 0
    4 2 4 1 5 4 3 2 1
    Output:
    5

     完全懒得自己写题解……

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int t,l,n,pre,now,x,y,ans;
     7 int c[210][210],p[2010],f[2][210][210];
     8 int main(){
     9     scanf("%d",&t);
    10     while(t){
    11         t--;
    12         memset(c,0,sizeof(c));
    13         memset(f,0x3f3f3f3f,sizeof(f));
    14         scanf("%d%d",&l,&n);
    15         for(int i=1;i<=l;i++)
    16             for(int j=1;j<=l;j++) scanf("%d",&c[i][j]);
    17         for(int i=1;i<=n;i++) scanf("%d",&p[i]);
    18         f[0][1][2]=c[3][p[1]];
    19         f[0][1][3]=c[2][p[1]];
    20         f[0][2][3]=c[1][p[1]];
    21         pre=1;now=0;
    22         for(int ii=2;ii<=n;ii++){
    23             pre^=1;
    24             now^=1;
    25             memset(f[now],0x3f3f3f3f,sizeof(f[now]));
    26             x=p[ii-1];
    27             y=p[ii];
    28             for(int i=1;i<=l;i++)
    29                 for(int j=1;j<=l;j++)
    30                     if(i!=j&&i!=x&&j!=x){
    31                         f[now][j][i]=f[now][i][j]=min(f[now][i][j],f[pre][i][j]+c[x][y]);
    32                         f[now][x][i]=f[now][i][x]=min(f[now][x][i],f[pre][i][j]+c[j][y]);
    33                         f[now][x][j]=f[now][j][x]=min(f[now][x][j],f[pre][i][j]+c[i][y]);
    34                     }
    35         }
    36         ans=0x3f3f3f3f;
    37         for(int i=1;i<=l;i++)
    38             for(int j=1;j<=l;j++)
    39                 if(i!=j&&i!=p[n]&&j!=p[n]) ans=min(ans,f[now][i][j]);
    40         printf("%d
    ",ans);
    41     }
    42     return 0;
    43 }
    
    
  • 相关阅读:
    数据结构与算法之PHP实现二叉树的遍历
    数据结构与算法之二叉树的基本概念和类型
    JS实现下拉单的二级联动
    数据结构与算法之PHP实现队列、栈
    数据结构与算法之PHP实现链表类(单链表/双链表/循环链表)
    数据结构与算法之数组、链表、队列、栈
    大型网站架构总结
    MySQL分库分表
    C基础 那些年用过的奇巧淫技
    C高级 服务器内核分析和构建 (一)
  • 原文地址:https://www.cnblogs.com/zwube/p/7108122.html
Copyright © 2011-2022 走看看