# 洛谷4049 [jsoi2007]合金

## 建图后利用floyd原理求最小代价

Posted by yjjr's blog on October 21, 2018 7 minutes to read

# 题目

## 输入输出样例

### 输入样例#1

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


### 输出样例#1

5


# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
#define inf 0x3f3f3f
#define eps 1e-9
const int maxn=1e3+6;
struct P{double x,y;}a[maxn],b[maxn];
int Map[maxn][maxn],n,m,ans;double k;
double operator * (P a,P b){return a.x*b.y-a.y*b.x;}
P operator - (P a,P b){P t;t.x=a.x-b.x;t.y=a.y-b.y;return t;}
inline bool col(P u,P v){
if(u.x>v.x)swap(u,v);
rep(i,1,m)if(b[i].x<u.x||b[i].x>v.x)return 0;
if(u.y>v.y)swap(u,v);
rep(i,1,m)if(b[i].y<u.y||b[i].y>v.y)return 0;
return 1;
}
inline int judge(P x,P y){
int c1=0,c2=0;
rep(i,1,m){
double t=(y-x)*(b[i]-x);
if(t>eps)c1++;
if(t<-eps)c2++;
if(c1*c2)return 0;
}
if(!c1&&!c2&&col(x,y)){ans=2;return -1;}
if(c1)return 1;if(c2)return 2;return 3;
}
void solve(){
rep(i,1,n)
rep(j,i+1,n){
int flag=judge(a[i],a[j]);
if(flag==-1)return;
if(flag==1)Map[i][j]=1;
else if(flag==2)Map[j][i]=1;
else if(flag==3)Map[i][j]=Map[j][i]=1;
}
}
void floyd(){
rep(k,1,n)rep(i,1,n)rep(j,1,n)Map[i][j]=min(Map[i][j],Map[i][k]+Map[k][j]);
}
int main(){
rep(i,1,n)scanf("%lf%lf%lf",&a[i].x,&a[i].y,&k);
rep(i,1,m)scanf("%lf%lf%lf",&b[i].x,&b[i].y,&k);
int flag=1;mem(Map,inf);ans=1e9;
rep(i,1,n)if(fabs(a[i].x-a[1].x)>eps||fabs(a[i].y-a[1].y)>eps)flag=0;
rep(i,1,m)if(fabs(b[i].x-a[1].x)>eps||fabs(b[i].y-a[1].y)>eps)flag=0;
if(flag)ans=1;
solve();floyd();
if(ans<1e9){cout<<ans<<endl;return 0;}
rep(i,1,n)ans=min(ans,Map[i][i]);
if(ans==1e9||ans<=2)puts("-1");else cout<<ans<<endl;
return 0;
}