1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<bits/stdc++.h> using namespace std; const int N = 1e3 + 10; const int inf = 1e18; int n; double x[N], y[N]; struct Loc { int l, r, c; Loc(int l = 0, int r = 0, int c = 0): l(l), r(r), c(c) {} } loc[N][N][2]; double dp[N][N][2]; double get_dis(int a, int b) { return sqrt(pow(x[a] - x[b], 2) + pow(y[a] - y[b], 2)); } int check(int x) { if (x <= 0) x += n; if (x > n) x -= n; return x; } int m; double ans=inf; Loc Ans; bool mn(double &x, double y) { if (x > y) x = y; return x == y; } void update(int l, int r) { if (l && mn(dp[l][r][0], dp[l - 1][r][0] + get_dis(check(m + l), check(m + l - 1)))) loc[l][r][0] = Loc(l - 1, r, 0); if (l && mn(dp[l][r][0], dp[l - 1][r][1] + get_dis(check(m + l), check(m - r)))) loc[l][r][0] = Loc(l - 1, r, 1); if (r && mn(dp[l][r][1], dp[l][r - 1][0] + get_dis(check(m - r), check(m + l)))) loc[l][r][1] = Loc(l, r - 1, 0); if (r && mn(dp[l][r][1], dp[l][r - 1][1] + get_dis(check(m - r), check(m - r + 1)))) loc[l][r][1] = Loc(l, r - 1, 1); } int st[N]; int top; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i]; } double mx = -0x3f3f3f3f; for (int i = 1; i <= n; i++) if (y[i] > mx) mx = y[i], m = i; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) dp[i][j][0] = dp[i][j][1] = inf; dp[0][0][0] = dp[0][0][1] = 0; for (int len = 1; len < n; len++) { for (int l = 0; l <= len; l++) { int r = len - l; update(l,r); } } cout<<m<<" "; for(int i=0;i<n;i++) for(int k=0;k<2;k++) if(dp[i][n-i-1][k]<ans) ans=dp[i][n-i-1][k], Ans=Loc{i,n-i-1,k}; while(Ans.l>0||Ans.r>0){ st[top++]=Ans.c==0?check(m+Ans.l):check(m-Ans.r); Ans=loc[Ans.l][Ans.r][Ans.c]; } while(top){ top--; cout<<st[top]<<" "; } }
|