POJ 1873 The Fortified Forest (convex hull + enumeration)

Source: Internet
Author: User

Question: given some trees, each of which has coordinates, heights, and values, it is required to cut down some trees and enclose other trees with those wood, which requires the minimum cost and the same cost, cut down the minimum number of trees.
Because there are only 15 trees, the state is compressed to enumerate all the States, 2 ^ 15 power, and then find a convex bag for the remaining trees, find the perimeter of the convex bag, determine whether the wood is sufficient, and record the optimal solution.
The first WF question, water ~~~~
Notes:
When there are only one or two trees left, remember to make a special decision.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <cmath>
# Include <vector>
# Include <string>
# Include <algorithm>
# Include <queue>
# Define LL long
# Define eps 1e-7
# Define N 2000000
# Define MOD 1000000007.
# Define inf 1 <30
# Define zero (a) (fabs (double) (a) <eps)
Using namespace std;
Struct Point {
Int x, y, v, l;
} P [15];
Int n;
// Store uncut trees
Vector <Point>;
// Cross Product
Int xmul (Point p0, Point p1, Point p2 ){
Return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p1.y-Snapshot y) * (p2.x-Snapshot X );
}
// Calculate the distance
Double dist (Point p1, Point p2 ){
Return sqrt (double) (p2.x-p1.x) * (p2.x-p1.x) + (p2.y-p1.y) * (p2.y-p1.y ));
}
// Polar sorting
Bool cmp (Point p1, Point p2 ){
If (xmul (a [0], p1, p2)> 0) return true;
Else if (zero (xmul (a [0], p1, p2) & dist (a [0], p1) <dist (a [0], p2 )) return true;
Return false;
}
Double Grham_scan (int len ){
// If there is only one tree left, you do not need to enclose it.
If (a. size () = 1) return len;
// If there are only two shards left, the distance between the two is twice the sum. Note that the distance is twice the sum. You can see from the example below:
Else if (a. size () = 2) return len-dist (a [0], a [1]) * 2;
For (int I = 1; I <a. size (); I ++)
If (a [I]. y <a [0]. y | (a [I]. y = a [0]. y & a [I]. x <a [0]. x ))
Swap (a [0], a [I]);
Sort (a. begin () + 1, a. end (), cmp );
Vector <Point> s;
S. push_back (a [0]); s. push_back (a [1]); s. push_back (a [2]);
For (int I = 3; I <a. size (); I ++ ){
While (s. size ()> = 2 & xmul (s [s. size ()-2], s [s. size ()-1], a [I]) <eps)
S. pop_back ();
S. push_back (a [I]);
}
S. push_back (s [0]);
Double ans = 0;
// Calculate the convex circumference
For (int I = 0; I <s. size ()-1; I ++)
Ans + = dist (s [I], s [I + 1]);
Return len-ans;
}
Int main (){
Int cas = 0;
While (scanf ("% d", & n )! = EOF & n ){
For (int I = 0; I <n; I ++)
Scanf ("% d", & p [I]. x, & p [I]. y, & p [I]. v, & p [I]. l );
// The cost of the optimal solution, the number of trees to be cut down, and the optimal state
Int best_val = inf, best_num, best_state;
// The remaining wood in the optimal solution
Double best_extra;
// Enumeration
For (int I = 1; I <(1 <n)-1; I ++ ){
Int tmp_val = 0, tmp_len = 0;
A. clear ();
For (int j = 0; j <n; j ++ ){
If (! (1 <j) & I ))
A. push_back (p [j]);
Else {
Tmp_len + = p [j]. l;
Tmp_val + = p [j]. v;
}
}
// Small pruning
If (tmp_val> best_val) continue;
Double extra = Grham_scan (tmp_len );
// If extra <0, it is not enough.
If (extra> = 0 ){
If (tmp_val <best_val ){
Best_val = tmp_val;
Best_num = n-a.size ();
Best_state = I;
Best_extra = extra;
}
Else if (tmp_val = best_val & n-a.size () <best_num ){
Best_num = n-a.size ();
Best_state = I;
Best_extra = extra;
}
}
}
Printf ("Forest % d \ nCut these trees:", ++ cas );
For (int I = 0; I <n; I ++)
If (1 <I) & best_state) printf ("% d", I + 1 );
Printf ("\ nExtra wood: %. 2f \ n", best_extra );
}
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.