UVA 10911 Forming Quiz Teams

来源:互联网 发布:攻击路由器软件 编辑:程序博客网 时间:2024/05/23 11:58

大意:最优配对问题,集合上的动态规划。

思路:我做了好久,找一个BUG找了好久,似乎LYJ的白书有错误额,想睡觉了,还是要多理解才行啦。

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <string>#include <cmath>#include <algorithm>using namespace std;const int MAXN = 1<<21;const int INF = 0x3f3f3f3f;int n;struct node{int x, y;}a[MAXN];double d[MAXN];double dist(node a, node b){double ans = sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));return ans;}void read_case(){n *= 2;for(int i = 0; i < n; i++) scanf("%*s %d %d", &a[i].x, &a[i].y);}void dp(){d[0] = 0;for(int S = 1; S < (1<<n); S++){int i, j;d[S] = INF;for(i = 0; i < n; i++) if(S & (1<<i)) break;for(j = i+1; j < n; j++) if(S & (1<<j)){d[S] = min(d[S], dist(a[i], a[j]) + d[S^(1<<i)^(1<<j)]);}}printf("%.2lf\n", d[(1<<n)-1]);}void solve(){read_case();dp();}int main(){int times = 0;while(scanf("%d", &n) && n){printf("Case %d: ", ++times);solve();}return 0;}


原创粉丝点击