# bzoj1027 [JSOI2007]合金

## 1027: [JSOI2007]合金

Time Limit: 4 Sec  Memory Limit: 162 MB
Submit: 2671  Solved: 703
[Submit][Status][Discuss]

10 10
0.1 0.2 0.7
0.2 0.3 0.5
0.3 0.4 0.3
0.4 0.5 0.1
0.5 0.1 0.4
0.6 0.2 0.2
0.7 0.3 0
0.8 0.1 0.1
0.9 0.1 0
1 0 0
0.1 0.2 0.7
0.2 0.3 0.5
0.3 0.4 0.3
0.4 0.5 0.1
0.5 0.1 0.4
0.6 0.2 0.2
0.7 0.3 0
0.8 0.1 0.1
0.9 0.1 0
1 0 0

5

## Source

materia1.a*k+materia2.a*(1-k) == product.a

materia1.b*k+materia2.b*(1-k) == product.b

（因为我们使用叉积判断，所以要这样，注意，我们是逆时针看那个合法图形的）

http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html

  1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 typedef double DB;
16 #define For(i, s, t) for(int i = (s); i <= (t); i++)
17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
18 #define Rep(i, t) for(int i = (0); i < (t); i++)
19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
20 #define rep(i, x, t) for(int i = (x); i < (t); i++)
21 #define MIT (2147483647)
22 #define INF (1000000001)
23 #define MLL (1000000000000000001LL)
24 #define sz(x) ((int) (x).size())
25 #define clr(x, y) memset(x, y, sizeof(x))
26 #define puf push_front
27 #define pub push_back
28 #define pof pop_front
29 #define pob pop_back
30 #define ft first
31 #define sd second
32 #define mk make_pair
33 inline void SetIO(string Name) {
34     string Input = Name+".in",
35     Output = Name+".out";
36     freopen(Input.c_str(), "r", stdin),
37     freopen(Output.c_str(), "w", stdout);
38 }
39
40 const int N = 510;
41 const DB Eps = 1e-6;
42 inline int Cmp(DB a, DB b) ;
43 struct Point {
44     DB x, y;
45
46     inline bool operator ==(const Point &A) const {
47         if(!Cmp(x, A.x) && !Cmp(y, A.y)) return 1;
48         return 0;
49     }
50 } A[N], B[N];
51 int n, m;
52 int Dp[N][N], Map[N][N];
53
54 inline void Input() {
55     scanf("%d%d", &n, &m);
56     DB c;
57     For(i, 1, n) scanf("%lf%lf%lf", &A[i].x, &A[i].y, &c);
58     For(i, 1, m) scanf("%lf%lf%lf", &B[i].x, &B[i].y, &c);
59 }
60
61 inline int Cmp(DB a, DB b) {
62     if(a < b-Eps) return -1;
63     if(a > b+Eps) return 1;
64     return 0;
65 }
66
67 inline bool Cover(Point x) {
68     For(i, 1, m)
69         if(!(x == B[i])) return 0;
70     return 1;
71 }
72
73 inline void Exit(string Ans) {
74     cout<<Ans<<"\n";
75     exit(0);
76 }
77
78 inline DB Multi(Point O, Point A, Point B) {
79     return (A.x-O.x)*(B.y-O.y)-(A.y-O.y)*(B.x-O.x);
80 }
81
82 inline bool Couple(Point O, Point A) {
83     For(i, 1, m) {
84         if(Cmp(Multi(O, A, B[i]), 0.0)) return 0;
85         DB Left = O.x, Right = A.x, Up = O.y, Down = A.y;
86         if(Left > Right) swap(Left, Right);
87         if(Down > Up) swap(Up, Down);
88         if(Cmp(B[i].x, Left) < 0 || Cmp(B[i].x, Right) > 0 ||
89             Cmp(B[i].y, Down) < 0 || Cmp(B[i].y, Up) > 0) return 0;
90     }
91     return 1;
92 }
93
94 inline bool Judge(Point O, Point A) {
95     For(i, 1, m)
96         if(Cmp(Multi(O, A, B[i]), 0.0) < 0) return 0;
97     return 1;
98 }
99
100 inline void Solve() {
101     if(!m) Exit("0");
102     For(i, 1, n)
103         if(Cover(A[i])) Exit("1");
104     For(i, 1, n)
105         For(j, i+1, n)
106             if(Couple(A[i], A[j])) Exit("2");
107
108     For(i, 1, n)
109         For(j, 1, n) Dp[i][j] = Map[i][j] = 10000;
110     For(i, 1, n)
111         For(j, 1, n) {
112             if(i == j) continue;
113             if(Judge(A[i], A[j])) Dp[i][j] = Map[i][j] = 1;
114         }
115
116     int Ans = 10000;
117     For(k, 1, n) {
118         For(i, 1, k-1)
119             For(j, 1, k-1)
120                 Ans = min(Ans, Dp[i][j]+Map[j][k]+Map[k][i]);
121
122         For(i, 1, n)
123             For(j, 1, n)
124                 Dp[i][j] = min(Dp[i][j], Dp[i][k]+Dp[k][j]);
125     }
126
127     if(Ans > n) Ans = -1;
128     printf("%d\n", Ans);
129 }
130
131 int main() {
132     #ifndef ONLINE_JUDGE
133     SetIO("1027");
134     #endif
135     Input();
136     Solve();
137     return 0;
138 }
View Code

posted @ 2015-09-01 18:20  yanzx6  阅读(164)  评论(0编辑  收藏  举报