UVA 10911 Forming Quiz Teams
2015-08-13 22:55
405 查看
点击打开链接
思路:状态压缩,用一个整数的二进制来表示分组状态,然后枚举
思路:状态压缩,用一个整数的二进制来表示分组状态,然后枚举
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int n; struct note { double x,y; }a[20]; double dp[1<<21]; double len[20][20]; double sum; double dis(note a,note b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double slove (int s) { if(dp[s]!=-1) return dp[s]; dp[s]=5656565; for(int i=0;i<n;i++) { if(s&(1<<i)) { for(int j=0;j<n;j++) { if((i!=j)&&(s&(1<<j))) { dp[s]=min(slove(s^(1<<i)^(1<<j))+len[i][j],dp[s]); } } } } return dp[s]; } int main() { freopen("in.txt","r",stdin); int ans=1; while(scanf("%d",&n)!=EOF&&n) { char hhh[30]; n=n*2; for(int i=0;i<n;i++) scanf("%s%lf%lf",hhh,&a[i].x,&a[i].y); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { len[i][j]=len[j][i]=dis(a[i],a[j]); } } int s=(1<<n)-1; for(int i=1;i<=s;i++) dp[i]=-1; dp[0]=0; slove(s); printf("case %d: %.2lf\n",ans++,dp[s]); } }
相关文章推荐
- IBM Bluemix云计算大会见闻
- gradle 配置文件 build.gradle 属性详解
- UI 07 _ 导航视图控制器 与 属性传值
- Innodb/MyISAM在自增/UUID主键下的性能与索引空间比较
- Havok_2014-1-0_Pc_Xs_User_Guide(2.2.2-模拟仿真一个Physics2012世界)
- Havok_2014-1-0_Pc_Xs_User_Guide(1.5.7-计时器)
- UIViewController _loadViewFromNibNamed:bundle:
- -[UIViewController _loadViewFromNibNamed:bundle
- spoj1182 Sorted bit squence
- UIPageControl 与 UIScrollView
- Android中Looper的quit方法和quitSafely方法
- UVa 11955 I Can Guess the Data Structure!
- The Unique MST prim(次小生成树)
- 【转载】UITableView
- iOS开发 -- UILable详解
- iOS开发 -- UIView详解
- KVC(Key-value coding)机制
- UIPageViewController
- POJ 题目3481 Double Queue(SBT ro map)
- UI11_BLOCK传值