BZOJ 1027 JSOI 2007 合金 计算几何+最小环

16 篇文章 0 订阅
1 篇文章 0 订阅

题目大意

给出一些由三种金属组成的原料,用这些原料去组成用户需要的一定比例的金属。问要达成用户的需求,最少需要多少种原料。

思路

这是隐藏的很好的一道计算几何题。因为 x+y+z=1  ,所以 z  一维是没有用的,我们只需要x  y  <script id="MathJax-Element-106" type="math/tex">y</script>两个坐标就可以表示一种金属了。
然后我们把这些点放在平面上,发现两种不同的金属能够组成的范围是这两个点所构成的有向线段的左侧,这样我们求出所有满足要求的这样的有向线段,之后跑Floyd来求出最小环,就是我们需要的答案。

CODE

#define _CRT_SECURE_NO_WARNINGS

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1010
#define INF 0x3f3f3f3f
#define EPS 1e-10
using namespace std;
#define INRANGE(x, y, c) ((c <= x && c >= y) || (c <= y && c >= x))

struct Point{
    double x, y;

    Point(double _, double __):x(_), y(__) {}
    Point() {}
    Point operator -(const Point &a)const {
        return Point(x - a.x, y - a.y);
    }
    void Read() {
        scanf("%lf%lf%*lf", &x, &y);
    }
}src[MAX], ask[MAX];

inline double Cross(const Point &p1, const Point &p2)
{
    return p1.x * p2.y - p1.y * p2.x;
}

struct Line{
    Point p, v, _p;

    Line(const Point &_, const Point &__):p(_), v(__ - _), _p(__) {}
    Line() {}
    bool OnLine(const Point &a)const {
        if(fabs(Cross(p - a, v)) > EPS)
            return false;
        if(p.x == _p.x)
            return INRANGE(p.y, _p.y, a.y);
        return INRANGE(p.x, _p.x, a.x);
    }
    bool OnLeft(const Point &a)const {
        return Cross(p - a, v) > 0 || OnLine(a);
    }
};

int cnt, asks;
int map[MAX][MAX];

inline bool Same(int p)
{
    for(int i = 1; i <= asks; ++i)
        if(ask[i].x != src[p].x || ask[i].y != src[p].y)
            return false;
    return true;
}

bool SpecialJudge()
{
    if(!asks) {
        puts("0");
        return true;
    }
    for(int i = 1; i <= cnt; ++i)
        if(Same(i)) {
            puts("1");
            return true;
        }
    return false;
}

int Floyd()
{
    for(int k = 1; k <= cnt; ++k)
        for(int i = 1; i <= cnt; ++i)
            for(int j = 1; j <= cnt; ++j)
                map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
    int ans = INF;
    for(int i = 1; i <= cnt; ++i)
        ans = min(ans, map[i][i]);
    return ans == INF ? -1:ans;
}

int main()
{
    cin >> cnt >> asks;
    for(int i = 1; i <= cnt; ++i)
        src[i].Read();
    for(int i = 1; i <= asks; ++i)
        ask[i].Read();
    if(SpecialJudge())
        return 0;
    memset(map, 0x3f, sizeof(map));
    for(int i = 1; i <= cnt; ++i)
        for(int j = 1; j <= cnt; ++j) {
            if(i == j)  continue;
            bool flag = true;
            Line t(src[i], src[j]);
            for(int k = 1; k <= asks; ++k)
                if(!t.OnLeft(ask[k])) {
                    flag = false;
                    break;
                }
            map[i][j] = flag ? 1:INF;
        }
    cout << Floyd() << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值