bitonic tour luogu1523

 1 #include<cstdio> 
 2 #include<cstring>
 3 #include<cmath>
 4 #include<ctime>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<set>
 9 #define inf (0x7fffffff)
10 #define l(a) ((a)<<1)
11 #define r(a) ((a)<<1|1)
12 #define b(a) (1<<(a))
13 #define T_min (1e-8)
14 #define R (0.9998)
15 #define e (2.718281828459045)
16 #define pi (3.14159265358979323846)
17 #define rep(i,a,b) for(int i=a;i<=(b);i++)
18 #define clr(a) memset(a,0,sizeof(a))
19 typedef long long ll;
20 typedef unsigned long long ull;
21 using namespace std;
22 int readint(){
23     int t=0,f=1;char c=getchar();
24     while(!isdigit(c)){
25         if(c=='-') f=-1;
26         c=getchar();
27     }
28     while(isdigit(c)){
29         t=(t<<3)+(t<<1)+c-'0';
30         c=getchar();
31     }
32     return t*f;
33 }
34 const int maxn=1009;
35 int n;
36 double d[maxn][maxn];
37 struct node{
38     double x,y;
39     inline bool operator<(const node A)const{
40         return x<A.x||(x==A.x&&y<A.y);
41     }
42 }X[maxn];
43 double dis(int u,int v){
44     return sqrt((X[u].x-X[v].x)*(X[u].x-X[v].x)+(X[u].y-X[v].y)*(X[u].y-X[v].y)); 
45 }
46 int main(){
47     //freopen("#input.txt","r",stdin);
48     //freopen("#output.txt","w",stdout);
49     n=readint();
50     rep(i,1,n){
51         X[i].x=readint();X[i].y=readint();
52     }
53     sort(X+1,X+n+1);
54     //rep(i,1,n) printf("%d %d\n",X[i].x,X[i].y);
55     rep(i,1,n) rep(j,1,n) d[i][j]=1e30;d[1][2]=dis(1,2);
56     rep(j,2,n){
57         rep(i,1,j-1){
58             if(j==i+1){
59                 rep(k,1,i-1) d[i][j]=min(d[i][j],d[k][i]+dis(j,k));
60             }else d[i][j]=min(d[i][j],d[i][j-1]+dis(j,j-1));
61         }
62     }
63     double ans=1e30;
64     rep(i,1,n-1) ans=min(ans,d[i][n]+dis(i,n));
65     printf("%.2lf\n",ans);
66     //fclose(stdin);
67     //fclose(stdout);
68     return 0;
69 }
DP

 

posted @ 2017-11-29 00:25  ChenThree  阅读(159)  评论(0编辑  收藏  举报