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. 【JUnit4.10源码分析】5 Statement

    假设要评选JUnit中最最重要的类型.或者说核心,无疑是org.junit.runners.model.Statement.Runner等类型看起来热闹而已. package org.junit.ru ...

  2. Android:源码环境下移植第三方的apk内置到ROM(System Image)中

    1. 首先在vendor目录下新建一个the3rdapk的目录,将需要内置的apk丢进去,目录名自己随意定. 2. 在 build/target/product/common.mk最后面,在$(cal ...

  3. c# datagridviewcomboboxcell值无效的解决办法

    一直认为是数据库存储的数据和datagridviewcomboboxcell对不上导致,今天碰到两者对应上了,预览的时候还是提示错误, 查看了下网上其他大神的解决方法,是数据库字段类型有误,查看了下, ...

  4. UIApplication相关

    1,显示应用图标右上角的红色提示 application.applicationIconBadgeNumber = 10; 2.修改状态栏的类型 在当前控制器下设置 - (UIStatusBarSty ...

  5. POJ1985 DFS【STL__vector_的应用】

    vector     向量 相当于一个数组    在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capacit ...

  6. Chapter 6 装饰模式

    修饰模式,是面向对象编程领域中,一种动态地往一个类中添加新的行为的设计模式.就功能而言,修饰模式相比生成子类更为灵活,这样可以给某个对象而不是整个类添加一些功能. 例如:DataOutputStrea ...

  7. 全球在一个 level 上思考的价值观和想法是一样的(转)

    近日,福布斯中文版总编辑周建工对话马云,谈到腾讯频繁的大笔收购,马云点评称腾讯收购的所有的案子,老百性都看得懂,这就错了.战略就像买股票一样,如果老太太都开始买股票了,一定有问题. 以下是对话内容,转 ...

  8. SQLite数据转换成sql server数据

    需要将SQLite数据库里的数据导入到SQL Server,在网上搜了好久,没有找到一个方便实用的方法. 经过本人的细心琢磨实验,终于让我给找到一好的方法:使用CSV文件作为介质来做转换.现在记录下来 ...

  9. CSS - 解决使用浮动,父窗体不能撑高问题

    浮动的父级元素一定要用 clear 清除浮动,否则高度会无法撑开.

  10. linux学习过程中所需要看的一些书籍

    入门类 一直认为,在一个系统上学习开发之前,首先需要熟悉这个系统的使用.鉴于天朝的国情,绝大部分人第一个接触的操作系统就是Windows,因此对于这绝大部分人来说,如果要学习Linux开发,学会使用这 ...