tyvj-1061 Mobile Service Foundation dp__ Algorithm Competition Advanced Guide

Source: Internet
Author: User

A company has three mobile waiters. If there is a request somewhere, an employee has to go to that place (there are no other employees in that place) and only one employee can move at a certain moment. After being requested, he cannot move, and two employees are not allowed to appear in the same position. Moving an employee from p to Q takes C (p,q), C (p,p) = 0. The company must meet all the requests. The goal is to minimize the cost of the company. At the beginning three waiters were in position 1,2,3.

Input Format

The first line has two integers l,n (3<=l<=200, 1<=n<=1000). L is the number of positions; n is the number of requests. Each location is numbered from 1 to L. The lower L row contains a non-negative integer in each row. The number of J in line I+1 is C (i,j) and it is less than 2000.  The last line contains the number of n, which is the request list. output Format

A number m, which represents the minimum service cost.

Input Sample #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 Sample #1

5


Train of thought: the first thought is to open a three-dimensional array dp[i][j][k] represents the minimum cost that is formed when the J person is in the K position when executing the first request

Wrote a pseudo equation dp[i][j][que[i] = min (Dp[i][j][que[i]], dp[i-1][j][k]+cost[k][que[i]);

But this state transition equation is obviously a fatal mistake, and there is no guarantee that only one employee at a time can move

And then added one more dimension to find the bug and then change the idea.

Dp[i][j][k][z] represents the position of three persons at the time of the I request, respectively, J K Z

Such dp[i+1][que[i+1]][k][z] = min (Dp[i+1][que[i+1]][k][z], dp[i][j][k][z] + cost[j][que[i+1]);

And so on, the three state transition equations represent the minimum cost required for the 1th, 2, 3 waiters to go to que[i+1 from J, K, Z, respectively.

But this space complexity is far beyond our reach. 1000*200*200*200 's not going to work.

By looking at every moment there must be a waiter in que[i] This position means that one of the waiters is not required to store the location of the que[i.

Such que[0] = 3 Dp[0][1][2] = 0 Que[i+1][j][k] = min (Que[i+1][j][k],que[i][j][k] + cost[que[i]][que[i+1]);

The three state transition equations, respectively, represent three cases from J to que[i+1] from K to que[i+1] from Que[i to que[i+1]

So we can optimize the complexity of the space into 1000*200*200.

However, this also goes beyond the memory limit. It is known that the DP equation is only relevant to the previous one, that is, every time the i+1 is launched from I and it has nothing to do with 0~i

So we can use the scrolling array to store only the two neighboring moments, ignoring all of the previous States so that the complexity of the space becomes 2*200*200.

We'll be able to finish this problem successfully.

AC Code:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace
Std
int dp[1100][202][202];
#define INF 0x3f3f3f3f int cost[220][220];
int que[1100];
    int main () {memset (dp,inf,sizeof (DP));
    int n,q;
    CIN >> N >> q; 
        for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j +) {cin >> cost[i][j];
    for (int i = 1; I <= Q; i++) {cin >> que[i];
    } dp[0][1][2] = 0;
    Que[0] = 3;  for (int i = 0; i < Q; i++) {for (int j = 1; J <= N; j +) {for (int k = 1; k <= N; k++) {if (i%2==0) {dp[1][que[i]][k] = min (dp[1][que[i]][
                    K],DP[0][J][K]+COST[J][QUE[I+1]]);
                    Dp[1][j][que[i]] = min (dp[1][j][que[i]],dp[0][j][k]+cost[k][que[i+1]); Dp[1][j][k] = min (dp[1][j][k],dp[0][j][k]+cost[que[i]][qUE[I+1]]);
                    Dp[0][que[i]][k] = inf;
                    Dp[0][j][que[i]] = INF;
                Dp[0][j][k] = inf; else {Dp[0][que[i]][k] = min (dp[0][que[i]][k],dp[1][j][k]+cost[j][q
                    UE[I+1]]);
                    Dp[0][j][que[i]] = min (dp[0][j][que[i]],dp[1][j][k]+cost[k][que[i+1]);
                    Dp[0][j][k] = min (dp[0][j][k],dp[1][j][k]+cost[que[i]][que[i+1]);
                    Dp[1][que[i]][k] = inf;
                    Dp[1][j][que[i]] = INF;
                Dp[1][j][k] = inf;
    an int ans = inf}}} for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j +) {ans = min (ans,dp[q%2][i)
        [j]);
}} cout<<ans<<endl;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.