zoukankan      html  css  js  c++  java
  • 吃奶酪 状压dp

    题目描述

    房间里放着n块奶酪。一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处。

    输入输出格式

    输入格式:

    第一行一个数n (n<=15)

    接下来每行2个实数,表示第i块奶酪的坐标。

    两点之间的距离公式=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))

    输出格式:

    一个数,表示要跑的最少距离,保留2位小数。

    输入输出样例

    输入样例#1: 复制
    4
    1 1
    1 -1
    -1 1
    -1 -1
    输出样例#1: 复制
    7.41
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<time.h>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize(2)
    using namespace std;
    #define maxn 200005
    #define inf 0x7fffffff
    //#define INF 1e18
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    #define mclr(x,a) memset((x),a,sizeof(x))
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-5
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    
    inline int rd() {
    	int x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    int sqr(int x) { return x * x; }
    
    
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    int n;
    struct node {
    	double x, y;
    }nd[20];
    
    double dp[17][(1 << 16) + 2];
    
    double dis(int i, int  j) {
    	return 1.0*sqrt((nd[i].x - nd[j].x)*(nd[i].x - nd[j].x) + (nd[i].y - nd[j].y)*(nd[i].y - nd[j].y));
    }
    
    int main()
    {
    	//	ios::sync_with_stdio(0);
    	n = rd(); ms(nd); mclr(dp, 127);
    	for (int i = 1; i <= n; i++) {
    		rdlf(nd[i].x); rdlf(nd[i].y);
    	}
    	for (int S = 0; S <= (1 << n) - 1; S++) {
    		for (int i = 1; i <= n; i++) {
    			if ((S&(1 << (i - 1))) == 0)continue;
    			if (S == (1 << (i - 1))) {
    				dp[i][S] = 0; continue;
    			}
    			for (int j = 1; j <= n; j++) {
    				if (i == j)continue;
    				if (S&(1 << (j - 1))) {
    					dp[i][S] = min(dp[i][S], 1.0*dis(i, j) + dp[j][S - (1 << (i - 1))]);
    				}
    			}
    		}
    	}
    	double MIN = 1.0*inf;
    	for (int i = 1; i <= n; i++) {
    		MIN = min(MIN, dis(0, i) + dp[i][(1 << n) - 1]);
    	}
    	printf("%.2lf
    ", 1.0*MIN);
    	return 0;
    }
    
  • 相关阅读:
    矩阵论基础 3.1初等变换
    最优化理论与方法 9 二次规划
    最优化理论与方法 10 罚函数法
    矩阵论基础 2.5 用Matlab实现矩阵的基本运算
    最优化理论与方法 目录
    UG OPEN API编程基础 12UIStyler对话框
    第十四章 达朗伯原理 1
    矩阵论基础 2.3 方阵的几种运算
    矩阵论基础 3.5 用Matlab求解线性方程组
    测试一下博客的html代码机制
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10488386.html
Copyright © 2011-2022 走看看