POJ 3565 Ants (Computational ry, KM)

Source: Internet
Author: User

Question: give some ant's points, give some tree points, correspond to each other, make their connections do not intersect, and output a scheme.
You can assume a combination at will, and then judge it by two. If yes, It is exchanged until all are not intersecting.
This idea is novel and called the adjustment idea in computational ry.
[Cpp]
# Include <iostream>
# Include <fstream>
# Include <iomanip>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <cstdlib>
# Include <cmath>
# Include <set>
# Include <map>
# Include <queue>
# Include <stack>
# Include <string>
# Include <vector>
# Include <sstream>
# Include <cassert>
# Define LL long
# Define eps 1e-8
# Define inf 10000
# Define zero (a) fabs (a) <eps
# Define N 20005
Using namespace std;
Struct Point {
Double x, y;
} Ant [105], tree [105];
Double xmul (Point p0, Point p1, Point p2 ){
Return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p1.y-Snapshot y) * (p2.x-Snapshot X );
}
Bool SegmentAcross (Point s1a, Point s1b, Point s2a, Point s2b ){
If (max (s1a. x, s1b. x)> = min (s2a. x, s2b. x) & max (s2a. x, s2b. x)> = min (s1a. x, s1b. x)
& Max (s1a. y, s1b. y)> = min (s2a. y, s2b. y) & max (s2a. y, s2b. y)> = min (s1a. y, s1b. y ))
If (xmul (s1a, s1b, s2a) * xmul (s1a, s1b, s2b) <-eps)
If (xmul (s2a, s2b, s1a) * xmul (s2a, s2b, s1b) <-eps)
Return true;
Return false;
}
Int main (){
Int n;
While (scanf ("% d", & n )! = EOF ){
For (int I = 0; I <n; I ++)
Scanf ("% lf", & ant [I]. x, & ant [I]. y );
For (int I = 0; I <n; I ++)
Scanf ("% lf", & tree [I]. x, & tree [I]. y );
Int match [105];
For (int I = 0; I <n; I ++)
Match [I] = I;
While (1 ){
Bool OK = true;
For (int I = 0; I <n; I ++)
For (int j = 0; j <n; j ++ ){
If (I = j) continue;
If (SegmentAcross (ant [I], tree [match [I], ant [j], tree [match [j]) {
Swap (match [I], match [j]);
OK = false;
}
}
If (OK) break;
}
For (int I = 0; I <n; I ++) printf ("% d \ n", match [I] + 1 );
}
Return 0;
}

Another way is to use the minimum permission matching of KM.
The reason is that the matching of the minimum weight must be inconsistent.
If AB and CD intersect with E, then AC <AE + EC and BD <BE + ED, then AB and CD are definitely not the minimum match.
But I don't know why the TLE, The KM is not familiar with it, maybe there is a problem, at least it can be done, generally within Ms.
[Cpp]
# Include <iostream>
# Include <fstream>
# Include <iomanip>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <cstdlib>
# Include <cmath>
# Include <set>
# Include <map>
# Include <queue>
# Include <stack>
# Include <string>
# Include <vector>
# Include <sstream>
# Include <cassert>
# Define LL long
# Define eps 1e-5
# Define inf 1ll <50
# Define zero (a) fabs (a) <eps
# Define N 20005
Using namespace std;
Struct Point {
Double x, y;
} Ant [105], tree [105];
Double path [1, 101] [2, 101];
Int cnt;
Double lx [101], ly [101];
Int match [101];
Double slack;
Bool v_x [101], v_y [101];
Double dist (Point p1, Point p2 ){
Return sqrt (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));
}
Bool dfs (int k ){
V_x [k] = true;
Double temp;
For (int I = 0; I <cnt; I ++ ){
If (! V_y [I]) {
Temp = lx [k] + ly [I]-path [k] [I];
If (zero (temp )){
V_y [I] = true;
If (match [I] =-1 | dfs (match [I]) {
Match [I] = k;
Return true;
}
}
Else
Slack = max (temp, slack );
}
}
Return false;
}
Void km (){
For (int I = 0; I <cnt; I ++ ){
Lx [I] = inf;
Ly [I] = 0;
For (int j = 0; j <cnt; j ++)
Lx [I] = min (path [I] [j], lx [I]);
}
Memset (match,-1, sizeof (match ));
For (int I = 0; I <cnt; I ++ ){
While (1 ){
Memset (v_x, false, sizeof (v_x ));
Memset (v_y, false, sizeof (v_y ));
Slack =-inf;
If (dfs (I ))
Break;
For (int j = 0; j <cnt; j ++ ){
If (v_x [j]) lx [j]-= slack;
If (v_y [j]) ly [j] + = slack;
}
}
}
}
Int main (){
While (scanf ("% d", & cnt )! = EOF ){
For (int I = 0; I <cnt; I ++)
Scanf ("% lf", & ant [I]. x, & ant [I]. y );
For (int I = 0; I <cnt; I ++)
Scanf ("% lf", & tree [I]. x, & tree [I]. y );
Memset (path, 0, sizeof (path ));
For (int I = 0; I <cnt; I ++)
For (int j = 0; j <cnt; j ++)
Path [I] [j] = dist (tree [I], ant [j]);
Km ();
For (int I = 0; I <cnt; I ++)
Printf ("% d \ n", match [I] + 1 );
}
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.