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. 《Android第一行代码》笔记

    学习Android开发差点儿相同有两年时间了.期间也做了大大小小的一些项目.近来抽出闲暇想把Android基础强化一下,之前在网上看到了郭霖郭大神的几篇博客.从中受益不少.于是花了近一周时间看完了郭神 ...

  2. 一个例子:HelloWorld

    作为C语言来说,我是用的是QT Creator作为开发工具. 事实上使用什么工具无所谓.重要的是学到实用的知识. 第一个实例程序就是HelloWorld程序.上代码: 版权声明:您好,转载请离开我的博 ...

  3. 制作Orcad的变种BOM(Variant BOM)

    通常在Orcad中画的原理图并不仅仅是用于一款产品.比如一个控制器原理图,可能相应着很多款子产品线,而这些子产品线之间的差别就是通讯口组件不同,少焊几个芯片,或者仅仅是少焊几个电阻. 可是这样交付生产 ...

  4. 2057 A + B Again

    A + B Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. 如何设置ssh安全只允许用户从指定的IP登陆

    原文链接: 如何设置ssh安全只允许用户从指定的IP登陆 由于开发上传文件需要  在服务器上开启  允许用户名和密码ssh登录.这样不太安全.百度后参考文章现在ssh用户名和密码登录的ip. 登录服务 ...

  6. QQ与我联系

    第一种 <a href=" http://sighttp.qq.com/cgi-bin/check?sigkey=ee8bdb91c04a9ae912a305a5a2461a0d8d6 ...

  7. JVM -- 类加载

    学习自周志明老师的<深入理解Java虚拟机>第二版 类的加载时机 如上图所示: 类从被加载到虚拟机内存中开始,直到卸载出内存为止,它的整个生命周期包括了: 加载.验证.准备.解析.初始化. ...

  8. Oracle中奇怪的【不等于号】

    Oracle中奇怪的[不等于号]   在Oracle中,不等号有三种:<>,!=,^= 例如: select * from test where name<>'xn'.返回的结 ...

  9. Clojure学习01:开始起步

    我们先要了解下什么是Clojure,我们从它的特性来了解: 1.首先它是一门编程语言,并且它写的程序是运行在jvm上的,同java语言一样. 2.Clojure代码可以使用任意java类库,反之jav ...

  10. linux下I2C驱动

    2C协议规定了主机和从机的概念,在驱动中采用的多是适配器(主机)和设备(从机).首先,i2c规定  Bus    -> Algorithm  算法               Adapter   ...