UVA 10911 Forming Quiz Teams

Source: Internet
Author: User
Tags min

Original question:
You are been given the job of forming the quiz teams for the next ' MCA CPCI quiz '.
There is 2∗n students interested to participate and you has to form N teams, each team consisting of the members. The Since the members has to practice together and all the students want their members house as near as possible. Let x1be the distance between the houses of Group 1, x2be the distance between the houses of Group 2 and so on. sure the summation (X1+X2+X3+...+XN) is minimized.
Input
There'll is many cases in the input file. Each case is starts with an integer N (n≤8). The next 2∗n
Lines would given the information of the students. Each line starts with the students name, followed by
The X coordinate and then the Y coordinate. Both x, y is integers in the range 0 to 1000. Students
Name would consist of lowercase letters only and the length would be in most 20.
Input is terminated by a, 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
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
Sample Output
Case 1:118.4
Case 2:1.41

Effect:
N*2 you a point coordinate, now let you turn these points into N pairs, and finally ask for the minimum sum of the distance of each pair.

#include <bits/stdc++.h> using namespace std;
Double dp[65536];
struct Node {double x, y;};
Node NODE[20];
    Double dist (int a,int b) {double x=node[a].x-node[b].x;
    Double y=node[a].y-node[b].y;
return sqrt (x*x+y*y);
    } int main () {Ios::sync_with_stdio (false);
    int n,k=0;
        while (cin>>n,n) {string ss;
            for (int i=0;i<2*n;i++) {cin>>ss;
        cin>>node[i].x>>node[i].y;
        } memset (Dp,0,sizeof (DP));
            for (int s=1;s< (1<<n*2); s++) {Dp[s]=int_max;
            int i;
            for (i=0;i<n*2;i++) if (s& (1<<i)) break; for (int j=i+1;j<n*2;j++) if (s& (1<<j)) Dp[s]=min (Dp[s],dist (i,j) +dp[s^ (1&LT;&L
        T;i) ^ (1<<j)]);
        } cout<< "Case" <<++k<< ":"; Cout<<fixed<<setprecision (2) <<dp[(1<< n*2) -1]<<endl;
} return 0;
 }

Answer:
has heard of the state compression of this method, the transfer equation is very good to think out. and LRJ small petition and purple books are introduced, but the small petition and purple books seem to have errors, can see the errata. The
Transfer equation is dp[s]=min (dp[s],dp[s^i^j]+dist (I,J)) wherein I=max (s), where the light is represented by the State, because to enumerate to the current two points I and J, the State s in the first bit 1 as I, and then enumerate J.
In addition, this problem can be described as LRJ in a two-dimensional array to represent Dp[i][s]=min (Dp[i-1][s^i^j]+dist (I,J)), the transfer equation is no doubt, but I do not want to understand the code in the book in the enumeration of all the state s when the first point I know, That is, I must have a status of 1 in S.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.