Hust 1607 Triangles is a good question hash

Source: Internet
Author: User

TrianglesTime Limit: 1 Sec Memory Limit: 128 MBSubmissions: 115 Solved: 30 DescriptionYou are given a figure consisting of n points in a 2D-plane and m segments connecting some of them. we guarantee that any two segments don't share points before t their ends and there's no more than one segment between the same pair of points. please count the total number of triangles in the given figure. inputThere' Re multiple test cases. in each case: The first line contains two positive integers n and m. (n <= 200, m <= 20000) Each of the following n lines contains two real numbers xi and yi indicating the coordinates of the ith point. (-100000 <xi, yi <100000) Each of the following m lines contains four real numbers xi, yi, xj, yj. it means (xi, yi) and (xj, yj) are connected by a segment. we guarantee That these points are part of the given n points. outputFor each test case, print a single line contains the total number of triangles in the given figure. please see sample for more detailsSample Input4 50 01 12 01 00 0 1 11 1 2 02 0 1 01 0 01 1 1 0 Sample Output3HINTSourceThe 7th (2012) ACM Programming Contest of HUSTProblem Setter: zhou is under the copy of the great gods do not need to despise the amount of first save the feeling that this question is very good, it is necessary to save the question: to some vertices coordinates and the points in the composition To find the number of triangles that can be formed by these line segments. Train of Thought: Since the number of points is only 100, we can enumerate three points, O (n ^ 3 ); however, there are some conditions: the three line segments composed of the three points must be in the line segment given above. How can this problem be determined? Here we can use the hash coordinate method to hash the coordinate of a point into a number. This is very simple, that is, to add the x and y coordinates of each point to 100000, finally, key = x * 200000 + y. Because y does not exceed 200000, the coordinates of each point are different, then we can create a ing between the key and the coordinate sequence number. Then we can create an adjacent matrix for the 100 point to start each line segment when entering data, we can find the coordinates at both ends of a line segment to get its hash value, and use the ing relationship to find the sequence number of the point, and then assign the positions of the two sequence numbers in the adjacent matrix to 1, for example, the numbers of the two endpoints of a line segment are 1 and 2, and map [] [] is the link matrix, in this case, map [1] [2] = map [2] [1] = 1, just like adding an edge, in this way, we get the relationship between all vertices. Next we need to solve a problem, such as the parallel line segments AB and bc, then we can get a new line segment, that is, ac. To solve this problem, we can use the method of loose points. This is similar to the floyd algorithm Inter-complexity O (n ^ 3). For details, refer to the Code [cpp] # include <math. h> # include <stdio. h> # include <string. h ># include <map> using namespace std; struct node {double x, y ;}; node point [220]; double yy [220]; map <double, int> pp; double get (double x, double y) // point hash {// x + = 100000; // y + = 100000; x * = 200000; x + = y; return x;} int mp [220] [220]; double dis (node a, node B) {return sqrt (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y ));} Int judgeline (node a, node B, node c) // you can determine the linestring. Note that if the slope is 0, you must separately judge the linestring. {double x, y, z, t; x =. x-c.x; y =. y-c.y; z = B. x-c.x; t = B. y-c.y; if (t <1e-8 & t>-1e-8) & (y <1e-8 & y>-1e-8) return 1; else {if (t <1e-8 & t>-1e-8) | (y <1e-8 & y>-1e-8) return 0; t = (x/y) -(z/t); return (t <1e-8 & t>-1e-8);} int subjudge (node a, node B, node c) // judge the triangle {double x, y, z; x = dis (a, B); y = dis (a, c); z = dis (B, c ); if (x + y-z> 1e-8 & x + z-y> 1e-8 & y + Z-x> 1e-8) return 1; else return 0;} int judge (int a, int B, int c) // determine whether the relationship between the three points meets {if (mp [a] [B] & mp [a] [c] & mp [B] [c]) return subjudge (point [a], point [B], point [c]); else return 0;} int main () {int n, m, a, B, I, j, k, sum; double x1, y1, x2, y2; while (scanf ("% d", & n, & m )! = EOF) {pp. clear (); for (I = 0; I <n; I ++) {scanf ("% lf", & point [I]. x, & point [I]. y); yy [I] = get (point [I]. x, point [I]. y); // vertex hash pp [yy [I] = I; // ing between vertex and serial number} memset (mp, 0, sizeof (mp )); for (I = 0; I <m; I ++) // establish the adjacent matrix {scanf ("% lf", & x1, & y1, & x2, & y2); a = pp [get (x1, y1)]; B = pp [get (x2, y2)]; mp [a] [B] = 1; mp [B] [a] = 1;} for (I = 0; I <n; I ++) // point loose {for (j = 0; j <n; j ++) {if (I! = J) {for (k = 0; k <n; k ++) {if (I! = K & I! = J & j! = K) {if (! Mp [j] [k] & mp [I] [j] & mp [I] [k] & judgeline (point [I], point [j], point [k]) mp [j] [k] = 1, mp [k] [j] = 1 ;}}} sum = 0; for (I = 0; I <n; I ++) // triangle enumeration and judgment for (j = I + 1; j <n; j ++) for (k = j + 1; k <n; k ++) sum + = judge (I, j, k); printf ("% d \ n ", sum);} 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.