zoukankan      html  css  js  c++  java
  • uva1084

    状压dp+凸包

    并没有看出来凸包的性质

    首先答案一定在凸包上,然后每个凸包的角加起来是一个圆,那么就相当于凸包周长加一个圆了。然后预处理,再状压dp计算即可。

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 10;
    const double pi = acos(-1);
    struct points {
        double x, y;
        bool friend operator < (points A, points B)
        {
            return A.x == B.x ? A.y < B.y : A.x < B.x;
        }
    } point[N * 10], p[N * 10];
    int n, m, cnt, top, kase;
    double dp[1 << N], d[1 << N];
    int st[N * 10];
    double cross(points x, points y, points z)
    {
        return (x.x - z.x) * (y.y - z.y) - (x.y - z.y) * (y.x - z.x);
    }
    double dis(points x, points y)
    {
        return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y));
    }
    double graham()
    {
        double ret = 0;
        top = 0;
        for(int i = 1; i <= cnt; ++i)
        {
            while(top > 1 && cross(p[i], p[st[top]], p[st[top - 1]]) >= 0) --top;
            st[++top] = i;
        }
        int lim = top;
        for(int i = cnt - 1; i; --i)
        {
            while(top > lim && cross(p[i], p[st[top]], p[st[top - 1]]) >= 0) --top;
            st[++top] = i;
        }
        for(int i = 1; i < top; ++i) ret += dis(p[st[i]], p[st[i + 1]]);
        return ret;
    }
    int main()
    {
        while(scanf("%d%d", &n, &m))
        {
            if(!n && !m) break;
            for(int i = 0; i < n; ++i) scanf("%lf%lf", &point[i].x, &point[i].y);
            sort(point, point + n);
            for(int i = 1; i < (1 << n); ++i) 
            {
                cnt = 0;
                for(int j = 0; j < n; ++j) if(i & (1 << j))
                    p[++cnt] = point[j];            
                dp[i] = graham() + 2 * m * pi;
            }
            for(int i = 1; i < (1 << n); ++i)
                for(int S = i; S; S = (S - 1) & i)
                    dp[i] = min(dp[i], dp[S] + dp[i ^ S]);
            printf("Case %d: length = %.2f
    ", ++kase, dp[(1 << n) - 1]);
        }
        return 0;
    }
  • 相关阅读:
    【学习笔记】【C语言】注释
    【学习笔记】【C语言】标识符
    【学习笔记】【C语言】关键字
    【学习笔记】【C语言】第一个C程序
    【学习笔记】Xcode常见设置
    【学习笔记】Mac OS X系统介绍
    【学习笔记】虚拟机安装Mac系统
    javascript 对象 + 数组
    SpringMVC实现原理及详解
    javaweb国际化
  • 原文地址:https://www.cnblogs.com/19992147orz/p/7139959.html
Copyright © 2011-2022 走看看