4th IIUC Inter-University Programming Contest, 2005

G

Forming Quiz Teams

Input: standard input
Output: standard output

Problemsetter: Sohel Hafiz

You have been given the job of forming the quiz teams for the next ‘MCA CPCI Quiz Championship’. There are2*N students interested to participate and you have to form teams, each team consisting of two members. Since the members have to practice together, all the students want their member’s house as near as possible. Let x1 be the distance between the houses of group 1, x2 be the distance between the houses of group 2 and so on. You have to make sure the summation (x1 + x2 + x3 + …. + xn) is minimized.

Input

There will be many cases in the input file. Each case starts with an integer N (N ≤ 8). The next 2*Nlines will given the information of the students. Each line starts with the student’s name, followed by thex coordinate and then the y coordinate. Both x, y are integers in the range 0 to 1000. Students name will consist of lowercase letters only and the length will be at most 20.

Input is terminated by a case where N is equal to 0.

Output

For each case, output the case number followed by the summation of the distances, rounded to 2 decimal places. Follow the sample for exact format.

Sample Input

Output for Sample Input

5
sohel 10 10
mahmud 20 10
sanny 5 5
prince 1 1
per 120 3
mf 6 6
kugel 50 60
joey 3 24
limon 6 9
manzoor 0 0
1
derek 9 9
jimmy 10 10
0

Case 1: 118.40
Case 2: 1.41

题意:给定n,有2*n个人,分成两人一组的n组,每个人在一个坐标上,要求出所有组的两人距离的和最小。

思路:明显的集合最优配对问题,i最为前i个人,s作为前i个人能组成的所有集合。j为前i个人中的一个人

这样状态转移方程为dp[i][s] =min(dp[i][s], dis(i, j) + dp[i - 1][s - {i} - {j}]。集合用二进制来表示。所以方程变为

dp[i][s] =min(dp[i][s], dis(i, j) + dp[i - 1][s ^ 1<<i ^ 1<<j]。但是这样做时间不是很理想。然后看了个详细解法

http://blog.csdn.net/accelerator_/article/details/11181905

里面讲得挺详细的。可以转化为1维。

代码:

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h> int n, i, j, s;
double dp[1<<20];
struct Student{
char name[105];
int x, y;
} stu[20]; double min(double a, double b) {
return a < b ? a : b;
} double dis(Student a, Student b) {
return sqrt((double)((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
} double dpp(int p) {
if (dp[p] != -1)
return dp[p];
dp[p] = INT_MAX;
int i, j;
for (i = n - 1; i >= 0; i --)
if (p & (1 << i))
break;
for (j = i - 1; j >= 0; j --)
if (p & (1 << j))
dp[p] = min(dp[p], dis(stu[i], stu[j]) + dpp(p ^ (1<<i) ^ (1<<j)));
return dp[p];
}
int main(){
int t = 1;
while (~scanf("%d", &n) && n) {
n *= 2;
s = 1<<n;
for (i = 1; i < s; i ++)
dp[i] = -1;
dp[0] = 0;
for (i = 0; i < n; i ++)
scanf("%s%d%d", stu[i].name, &stu[i].x, &stu[i].y); printf("Case %d: %.2lf\n", t ++, dpp(s - 1));
}
return 0;
}

UVA 10911 Forming Quiz Teams(dp + 集合最优配对问题)的更多相关文章

  1. uva 10911 - Forming Quiz Teams(记忆化搜索)

    题目链接:10911 - Forming Quiz Teams 题目大意:给出2 * n个选手的坐标, 要求将所有的选手分成n组, 每组两个人, 所有组的两个人之间的距离之和要最小, 输出最小值. 解 ...

  2. UVa 10911 - Forming Quiz Teams

    题目大意:给出2*n个点,将这些点配成n对,使得所有点对中两点的距离之和尽量小. 用一个整数的二进制形式表示一个集合的子集,以每个集合为状态进行状态转移.具体参见<算法竞赛入门经典>9.5 ...

  3. 集合上的动态规划---最优配对问题(推荐:*****) // uva 10911

    /* 提醒推荐:五星 刘汝佳<算法竞赛入门经典>,集合上的动态规划---最优配对问题 题意:空间里有n个点P0,P1,...,Pn-1,你的任务是把它们配成n/2对(n是偶数),使得每个点 ...

  4. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  5. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  6. UVA 10529 - Dumb Bones (概率dp)

    题目描述 You are trying to set up a straight line of dominos, standing on end, to be pushed over later f ...

  7. 【全网免费VIP观看】哔哩哔哩番剧解锁大会员-集合了优酷-爱奇艺-腾讯-芒果-乐视-ab站等全网vip视频免费破解去广告-高清普清电视观看-持续更新

    哔哩哔哩番剧解锁大会员-集合了优酷-爱奇艺-腾讯-芒果-乐视-ab站等全网vip视频免费破解去广告-高清普清电视观看-持续更新 前言 突然想看电视,结果 没有VIP 又不想花钱,这免费的不久来啦. 示 ...

  8. UVa 1009 Sharing Chocolate (数位dp)

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  9. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

随机推荐

  1. EBS 数据库预克隆日志

    ora02@[/u07/CCTEST02/db/tech_st/11.1.0/appsutil/scripts/CCTEST02_test01] $ T02_test01/StageDBTier_06 ...

  2. Linux内核态抢占机制分析(转)

    Linux内核态抢占机制分析  http://blog.sina.com.cn/s/blog_502c8cc401012pxj.html 摘 要]本文首先介绍非抢占式内核(Non-Preemptive ...

  3. Checkbox in DataList

    一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...

  4. html向servlet传乱码解决办法

    html 设置为utf-8格式 <meta http-equiv="content-type" content="text/html;charset=UTF-8&q ...

  5. yii_wiki_204_using-cjuidialog-to-edit-rows-in-a-cgridview(通过CJuiDialog在CGridView中修改行数据)

    /*** Using CJuiDialog to edit rows in a CGridView http://www.yiiframework.com/wiki/204/using-cjuidia ...

  6. 设计模式(七)组合模式Composite(结构型)

    设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...

  7. 基于visual Studio2013解决C语言竞赛题之0518回文数

     题目

  8. datanode启动后,在web50070port发现不到datanode节点(能力工场)

    直接上问题:这两天为了试验,安装了两套集群: (1)32位hadoop1集群(5个节点); (2)64位hadoop2集群(6个节点) 两个集群中都遇到过这种问题:在namenode正常启动hadoo ...

  9. opencv鼠标绘制直线 C++版

    因为需要在图片上标记直线,所以从网上找了相应的参考资料.但大多都是c风格的,于是自己就写了一个c++风格的. opencv2.4.11,win8.1,vs2013 #include <cv.h& ...

  10. [转]CentOS下性能监测工具 dstat

    原文链接:http://www.bkjia.com/Linuxjc/935113.html 参考链接:https://linux.cn/article-3215-1.html,http://lhfli ...