1151-buy or build (Binary enumeration subset + query set)

Source: Internet
Author: User

There is a problem with the translation in the lrj book. The book says that the cost between two points is the Euclidean distance between two points, and the question requires that the distance between two points is the square of the Euclidean distance between two points.

There is nothing to say about the rest. we need to pay attention to the problem of the binary enumeration subset in the bare and querying sets.

Binary enumeration subset:

For (INT I = 0; I <(1 <s); I ++) {/* s is the number of elements in the Set */For (Int J = 0; j <s; j ++) {If (! (S> J) & 1) continue; else {}}}

14054883 1151 Buy or build Accepted C ++ 0.118 10:37:07

If the comments are added, I will not explain them much:

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <stack> # include <queue> # include <map> # include <set> # include <list> # include <cmath> # include <string> # include <sstream> # include <ctime> using namespace STD; # DEFINE _ pI ACOs (-1.0) # define ESP 1e-9typedef long ll; typedef unsigned long ull; typedef pair <int, int> pill; /* ===================================================== ===== =================== Kinderriven ================================ ================================================= */ # define maxd 1100/* up to 1000 vertices */vector <int> array; int n, m, line_size; int Fa [maxd]; struct line {int L; int R; int cost; friend bool operator <(line P, line Q) {If (P. cost <q. cost) return true; else return false;} line [maxd * maxd]; struct point {int X; int y;} p [maxd]; struct POW {int cost; int size; int arr [Max D];} Q [15]; int _ dist (point P, point Q) {int x = P. x-Q. x; int y = P. y-Q. y; int ans = x * x + y * Y; return ans;} int find_father (int u) {return Fa [u] = u? U: Fa [u] = find_father (Fa [u]);} void Init () {for (INT I = 0; I <= N; I ++) fa [I] = I; return;} ll solve (INT U) {int now = 0;/* Number of edge added */INT cost = 0; Init (); for (INT I = 0; I <m; I ++) {If (! (U> I) & 1) continue;/* List-level enumeration subset */cost + = Q [I]. cost;/* package cost */For (Int J = 1; j <q [I]. size; j ++) {int T1 = find_father (Q [I]. arr [J]); int t2 = find_father (Q [I]. arr [J-1]); If (T1! = T2) {Fa [T1] = t2; now ++ ;}}for (INT I = 0; now <n-1; I ++) {int c = array [I];/* locate the Edge Number */INT _ T1 = line [C]. l;/* locate the two endpoints of the edge */INT _ t2 = line [C]. r; int T1 = find_father (_ T1); int t2 = find_father (_ T2); If (T1! = T2) {cost + = line [C]. cost; Fa [T1] = t2; now ++ ;}return cost ;}int main () {int t; scanf ("% d", & T ); while (t --) {array. clear (); LL _ ans = 0; scanf ("% d", & N, & M); Init (); For (INT I = 0; I <m; I ++) {scanf ("% d", & Q [I]. size, & Q [I]. cost); For (Int J = 0; j <q [I]. size; j ++) scanf ("% d", & Q [I]. arr [J]);} line_size = 0;/* obtain all edges */For (INT I = 1; I <= N; I ++) {scanf ("% d", & P [I]. x, & P [I]. y);/* output coordinate */F Or (Int J = 1; j <I; j ++) {/* Find 1 ~ The distance between I-1 point and I */line [line_size]. L = J; line [line_size]. R = I; line [line_size]. cost = _ dist (P [I], p [J]);/* calculate two points if the cost of the connection is required */line_size ++ ;}} /* find the shortest short path and record each side of the shortest path * // * sort the edges first */sort (line, line + line_size); For (INT I = 0, j = 0; j <n-1; I ++) {/* When the link is n-1, it means that N points are connected */int l = line [I]. l, r = line [I]. r;/* Find the two endpoints of a line segment */INT fa_l = find_father (L ); /* use the query set to find its connected component */INT fa_r = find_father (r); If (fa_l! = Fa_r) {/* if not the same connected component */Fa [fa_l] = fa_r;/* merge! */J ++;/* add an edge */_ ans + = line [I]. cost;/* fee */array. push_back (I);/* Add the smallest edge */}/* List-level enumeration subset! */For (INT I = 1; I <(1 <m); I ++) {ll ans = solve (I); _ ans = min (ANS, _ ans);} printf ("% LLD \ n", _ ans); If (T> 0) printf ("\ n");} return 0 ;}


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.