[TYVJ1061] Mobile service| Dynamic Planning | Scrolling array

Source: Internet
Author: User

P1061Mobile Service time: 1000ms/Space: 131072kib/java Class Name: Main describes a company with three mobile waiters. If there is a request somewhere, an employee must go to that place (where there are no other employees) and only one employee can move at a time. After being asked, he can move and not allow two employees to appear in the same position. To move an employee from P to Q, it takes C (p,q). This function is not necessarily symmetric, but C (p,p) = 0. The company must satisfy all requests. The goal is to minimize the cost of the company. The first line of input format has two integers l,n (3<=l<=200, 1<=n<=1000). L is the number of positions; n is the number of requests. Each position is numbered from 1 to L. The next L row contains l non-negative integers per row. The number of J in line i+1 indicates C (I,J), and it is less than 2000. The last line contains the n number, which is the request list. At first, three waiters were in position. The output format is a number m, which represents the minimum service cost. Test Sample 1 input
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
First [email Protected]_colin F[l][i][j][k] said to proceed to the first task, a person in I, a person in J, a person in K. Because f[l][][][] is only related to f[l-1][][][], the first dimension can be scrolled ~ but still a little general tle AH T T---------------------------------following worship Ws_ Colin-------------------------------because each task must be satisfied, you can pressure off one dimension and then change the transfer.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#defineINF 1000000000using namespacestd;intl,n,x,r,ans,a[205][205],f[2][205][205],t[1005];inlineintRead () {intA=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9') {if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {a=a*Ten+c-'0'; C=GetChar ();} returnA *F;}intMain () {L=read (); n=read ();  for(intI=1; i<=l;i++)         for(intj=1; j<=l;j++) A[i][j]=read ();  for(intI=1; i<=n;i++) t[i]=read (); Ans=inf;  for(intI=1; i<=l;i++)         for(intj=1; j<=l;j++) F[r][i][j]=inf; f[r][1][3]=a[2][t[1]]; f[r][2][3]=a[1][t[1]]; f[r][1][2]=a[3][t[1]];  for(intk=1; k<n;k++) {R^=1;  for(intI=1; i<=l;i++)             for(intj=1; j<=l;j++) F[r][i][j]=inf;  for(intI=1; i<=l;i++)             for(intj=1; j<=l;j++) {F[r][j][t[k]]=min (f[r][j][t[k]],f[r^1][i][j]+a[i][t[k+1]]); F[R][I][T[K]]=min (f[r][i][t[k]],f[r^1][i][j]+a[j][t[k+1]]); F[R][I][J]=min (f[r][i][j],f[r^1][i][j]+a[t[k]][t[k+1]]); }    }     for(intI=1; i<=l;i++)         for(intj=1; j<=l;j++) ans=min (ans,f[r][i][j]); printf ("%d", ans); return 0;}

[TYVJ1061] Mobile service| Dynamic Planning | Scrolling array

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.