Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100610Description
Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer with his friends decided to create their own home robot. As you may know most programmers like to drink beer when they gather together for a party. After the party there are a lot of empty bottles left on the table. So, it was decided to program robot to collect empty bottles from the table. The table is a rectangle with the length l and width w. Robot starts at the point (xr, yr) and n bottles are located at points (xi , yi) for i = 1, 2, . . . , n. To collect a bottle robot must move to the point where the bottle is located, take it, and then bring to some point on the border of the table to dispose it. Robot can hold only one bottle at the moment and for simplicity of the control program it is allowed to release bottle only at the border of the table. Bottle Bottle Robot l w x y You can assume that sizes of robot and bottles are negligibly small (robot and bottles are points), so the robot holding a bottle is allowed to move through the point where another bottle is located. One of the subroutines of the robot control program is the route planning. You are to write the program to determine the minimal length of robot route needed to collect all the bottles from the table.
Input
The first line of the input file contains two integer numbers w and l — the width and the length of the table (2 ≤ w, l ≤ 1000). The second line of the input contains an integer number n — the number of bottles on the table (1 ≤ n ≤ 18). Each of the following n lines contains two integer numbers xi and yi — coordinates of the i-th bottle (0 < xi < w; 0 < yi < l). No two bottles are located at the same point. The last line of the input file contains two integer numbers xr and yr — coordinates of the robot’s initial position (0 < xr < w; 0 < yr < l). Robot is not located at the same point with a bottle.
Output
Output the length of the shortest route of the robot. Your answer should be accurate within an absolute error of 10−6 .
Sample Input
3 4 2 1 1 2 3 2 1
Sample Output
5.60555127546399
HINT
题意
数据范围已经告诉我们了,这是状压DP……%
题解:
老老实实状压DP就好了
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <cstdio> #include <vector> #include <cmath> #include <cstring> using namespace std; const int maxn = 18 + 2; struct bottle { double x,y; }; int w , l , n , ed ; double stx,sty; bottle p[maxn]; double dp[19][(1<<18)+5]; bool arrived[19][(1<<18)+5]; inline double DIS(double x1,double y1,double x2,double y2) { return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * ( y2 - y1)); } double dfs(int x,int y) { if(arrived[x][y]) return dp[x][y]; arrived[x][y] = true; double & ans = dp[x][y] = 1e233; int sz = 0; if(x == n) { for(int i = 0 ; i < n ; ++ i) ans = min( ans , dfs(i , y ) + DIS(stx,sty,p[i].x,p[i].y)); return ans; } for(int i = 0 ; i < n ; ++ i) if((y>>i) & 1) sz++; if(sz == 1) return ans = min( min(p[x].x , (double)w - p[x].x) , min(p[x].y , (double)l - p[x].y)); //最后一个瓶子 else { double x1 = p[x].x; double y1 = p[x].y; for(int i = 0 ; i < n ; ++ i) if((y >> i) & 1) { if(i == x) continue; double x2 = p[i].x; double y2 = p[i].y; double res = 1e233; res = min( res , DIS(-x1,y1,x2,y2) ); res = min( res , DIS(x1 , -y1,x2,y2)); res = min( res , DIS(2.0*w - x1 , y1 , x2 , y2)); res = min( res , DIS(x1 , 2.0*l - y1,x2,y2)); ans = min( ans , dfs(i , y &(~(1<<x)) ) + res); } } return ans; } int main(int argc,char *argv[]) { freopen("kitchen.in","r",stdin); freopen("kitchen.out","w",stdout); scanf("%d%d%d",&w,&l,&n); for(int i = 0 ; i < n ; ++ i) scanf("%lf%lf",&p[i].x , &p[i].y); scanf("%lf%lf",&stx,&sty); memset(arrived , false , sizeof(arrived)); ed = 1 << n; printf("%.14lf\n",dfs(n,ed-1)); return 0; }