UVA - 10911 Forming Quiz Teams

题目大意:有 2 * n 个点,使其组成 n 对,求 n 对点集的最小距离之和.

解题思路:由于 2 * n 最大才 20,完全可以由数字的位来表示,每一个数字表示一个状态,然后才去记忆化搜索的方式得出结果。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int N;
double DP[1 << 17], dis[20][20];

double cal(int x, int y) {
    return sqrt(x * x + y * y);
}

double DPS(int S) {
    if (DP[S] || !S)
        return DP[S];
    int i;
    for (i = N - 1; i >= 0; i--)
        if (S & (1 << i)) break;
    DP[S] = 0x3f3f3f3f;
    for (int j = i - 1; j >= 0; j--)
        if (S & (1 << j))
            DP[S] = min(DP[S], dis[i][j] + DPS(S ^ (1 << i) ^ (1 << j)));
    return DP[S];
}

int main() {
    int x[20], y[20], kase = 0;
    while (scanf("%d", &N), N) {
        N <<= 1;
        for (int i = 0; i < N; i++)
            scanf("%*s%d%d", &x[i], &y[i]);

        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                dis[j][i] = cal(x[i] - x[j], y[i] - y[j]);
        memset(DP, 0, sizeof(DP));
        printf("Case %d: %.2lf\n", ++kase, DPS((1 << N) - 1));
    }
    return 0;
}
时间: 2024-10-24 19:21:08

UVA - 10911 Forming Quiz Teams的相关文章

uva10911 - Forming Quiz Teams(记忆化搜索)

题目:uva10911 - Forming Quiz Teams(记忆化搜索) 题目大意:给出N对点的坐标,然后将这2 * N个点分组,Xi代表第i组的点之间的距离,求sum(Xi)最小值. 解题思路:这里的点最多16个,如果暴力求解的话16!,会超时的.这里的点取和不取可以用0和1表示,这样的话所有的状态可以用二进制数X来表示.dp[X]  = Min (dp[newx] + d[i][j]):记录下状态来减少不必要的重复操作. i .j代表点的下标,这些点必须是X状态下没取过的,newx就是

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

算法入门经典大赛 Dynamic Programming

111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence LCS 674 - Coin Change 全然背包求方案数 10003  - Cutting Sticks 区间DP dp[l][r]代表分割l到r的最小费用 116 - Unidirectional TSP 简单递推 输出字典序最小解 从后往前推 10131 - Is Bigger Smarte

P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team

题目描述 After Farmer Don took up Frisbee, Farmer John wanted to join in the fun. He wants to form a Frisbee team from his N cows (1 <= N <= 2,000) conveniently numbered 1..N. The cows have been practicing flipping the discs around, and each cow i has a

小白书关于动态规划

10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=show_problem&category=114&problem=1133&mosmsg= Submission+received+with+ID+13297616 */ #include <cstdio> #include <string.h> #include&

斯坦福CS课程列表

http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principles. 3-5 Units. Introduces the essential ideas of computing: data representation, algorithms, programming "code", computer hardware, networking, s

Codeforces Round #552 (Div. 3) E C++

题目: E. Two Teams time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are nn students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the

UVA - 11609 Teams (排列组合数公式)

In a galaxy far far awaythere is an ancient game played among the planets. The specialty of the game isthat there is no limitation on the number of players in each team, as long asthere is a captain in the team. (The game is totally strategic, so som