[BZOJ 1027][JSOI2007]合金(計算幾何+Floyd最小環)

來源:互聯網
上載者:User

標籤:完全   esc   floyd   思路   計算幾何   連線   工作   枚舉   span   

Description

某公司加工一種由鐵、鋁、錫組成的合金。他們的工作很簡單。首先進口一些鐵鋁錫合金原材料,不同種類的
原材料中鐵鋁錫的比重不同。然後,將每種原材料取出一定量,經過融解、混合,得到新的合金。新的合金的鐵鋁
錫比重為使用者所需要的比重。 現在,使用者給出了n種他們需要的合金,以及每種合金中鐵鋁錫的比重。公司希望能
夠訂購最少種類的原材料,並且使用這些原材料可以加工出使用者需要的所有種類的合金。

Solution

今天考試T3的70分演算法似乎是這道原題(然而我沒做過QwQ)

思路值得學習一下(看似完全不相關的東西就這麼聯絡到了一起,它…它它它是道計算幾何?QwQ)

因為比重中a+b+c=1,所以c這一維完全不需要

我們把(a,b)看做它們在二維平面上的座標,我們發現一種合金能被兩種原材料合成若且唯若它在這兩種材料的連線的線段上

於是當有很多種原材料和很多種需要合成的合金時,我們要求的東西就是一個能將所有需要合成的合金包圍的點數最少的原材料點的凸包

枚舉兩個原材料點,如果所有的合金點都在這一有向邊的左側(或者點線上段上/點與點重合),就在這兩個點間連一條邊

然後Floyd求最小環

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#define eps 1e-8#define INF 0x3f3f3f3f using namespace std;int m,n,dis[505][505];struct dot{    double x,y;    dot(double x=0,double y=0):x(x),y(y){}}a[505],b[505];typedef dot Vector;Vector operator + (Vector v1,Vector v2){return Vector(v1.x+v2.x,v1.y+v2.y);}Vector operator - (Vector v1,Vector v2){return Vector(v1.x-v2.x,v1.y-v2.y);}int dcmp(double x){    if(fabs(x)<eps)return 0;    return x>0?1:-1;}bool operator == (dot p1,dot p2){return (dcmp(p1.x-p2.x)&&dcmp(p1.y-p2.y))==0;}double cross(Vector v1,Vector v2){return v1.x*v2.y-v2.x*v1.y;}bool judge(dot x,dot y,dot z){    if(x==y&&x==z)return true;    Vector v1=z-x,v2=y-x;    if(dcmp(cross(v1,v2))<0)return true;    if(dcmp(cross(v1,v2))==0&&dcmp(z.x-min(x.x,y.x))>=0&&dcmp(z.x-max(x.x,y.x))<=0&&dcmp(z.y-min(x.y,y.y))>=0&&dcmp(z.y-max(x.y,y.y))<=0)return true;    return false;}int main(){    scanf("%d%d",&m,&n);    for(int i=1;i<=m;i++)    {        double x,y,z;        scanf("%lf%lf%lf",&x,&y,&z);        a[i]=dot(x,y);       }    for(int i=1;i<=n;i++)    {        double x,y,z;        scanf("%lf%lf%lf",&x,&y,&z);        b[i]=dot(x,y);      }    memset(dis,0x3f,sizeof(dis));    for(int i=1;i<=m;i++)    for(int j=1;j<=m;j++)    {        bool f=1;        for(int k=1;k<=n;k++)        if(!judge(a[i],a[j],b[k]))        {f=0;break;}        if(f)dis[i][j]=1;    }    for(int k=1;k<=m;k++)    {        for(int i=1;i<=m;i++)        for(int j=1;j<=m;j++)        {            if(dis[i][j]>dis[i][k]+dis[k][j])            dis[i][j]=dis[i][k]+dis[k][j];        }    }    int ans=INF;    for(int i=1;i<=m;i++)    ans=min(ans,dis[i][i]);    printf("%d\n",ans==INF?-1:ans);    return 0;}

 

[BZOJ 1027][JSOI2007]合金(計算幾何+Floyd最小環)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.