ZOJ 2710 Two Pipelines

贪心。。。。


Two Pipelines

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

There are two oil pipelines going through Flatland. Each pipeline can be considered as a line on the plane.

Recently the president of Flatland has decided to bring oil to all n cities of Flatland. Therefore he would like to build pipes from each city to one of the pipelines. The minister of industry and education has provided him with data about demand for oil in each city. Transporting a barrel of oil for one kilometer costs 1 flatlar.

The president ordered to connect each city to the closest pipleine, but it turned out that there is one problem. One pipeline belongs to a strong Hrundi Empire, while the other belongs to Bordland Republic. Since president wants to keep good relationships with both countries, he must not give preference to one of the pipelines. That is, if there would be x cities connected to one pipeline and y cities connected to the other, |x - y| must not exceed c .

Help president to decide which city should be connected to which pipeline, so that he keeps his international reputation clean, and spends as few money as possible.

Input

There are mutiple cases in the input file.

The first line of the input file contains n and c (1 <= c <= n <= 200 ). The second line contains the description of the first pipeline --- coordinates of two different points that are lying on it:x1 , y1 , x2 , y2 . The third line describes the second pipeline in the same format.

The following n lines describe cities. Each city is described with three integer numbers: its coordinates and the demand for the oil in thousands of barrels per day (it does not exceed1000 ).

All coordinates are integer, given in kilometers and do not exceed 104 by absolute values. No two cities coincide. Pipelines do not coincide either.

There is an empty line after each case.

Output

For each city output 1 if it must be connected to the first pipeline, or 2 if it must be connected to the second pipeline.

There should be am empty line after each case.

Sample Input

3 1
0 0 10 0
0 5 7 5
-2 1 1
1 1 1
4 2 10

Sample Output

1 2 1



#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn=500;

struct CITY
{
    double x,y,c;
    double d1,d2;
    double cs1,cs2,dd;
    int belong,id;
}city[500];

struct POINT
{
    double x,y;
};

struct PIPE
{
    double x1,y1,x2,y2;
}pipe[2];

double Cross(POINT a,POINT b)
{
    return a.x*b.y-a.y*b.x;
}

double Length(POINT a)
{
    return sqrt(a.x*a.x+a.y*a.y);
}

double getDistan(PIPE g,POINT P)
{
    POINT A=(POINT){g.x1,g.y1},B=(POINT){g.x2,g.y2};
    POINT v1=(POINT){B.x-A.x,B.y-A.y},v2=(POINT){P.x-A.x,P.y-A.y};
    return fabs(Cross(v1,v2))/Length(v1);
}

int N,C;
int guanzhi[2];

bool cmp(CITY a,CITY b)
{
    return a.dd<b.dd;
}

bool cmp2(CITY a,CITY b)
{
    return a.id<b.id;
}

int main()
{
while(cin>>N>>C)
{
    memset(guanzhi,0,sizeof(guanzhi));
    memset(city,0,sizeof(city));

    double x1,y1,x2,y2;
    cin>>x1>>y1>>x2>>y2;
    pipe[0]=(PIPE){x1,y1,x2,y2};
    cin>>x1>>y1>>x2>>y2;
    pipe[1]=(PIPE){x1,y1,x2,y2};

    for(int i=0;i<N;i++)
    {
        double a,b,c;
        cin>>a>>b>>c;
        city[i].x=a;city[i].y=b;
        city[i].c=c;city[i].id=i;
        POINT p=(POINT){a,b};
        city[i].d1=getDistan(pipe[0],p); city[i].cs1=city[i].d1*c;
        city[i].d2=getDistan(pipe[1],p); city[i].cs2=city[i].d2*c;
        city[i].dd=fabs(city[i].cs1-city[i].cs2);
        if(city[i].cs1>city[i].cs2)
        {
            city[i].belong=1;
            guanzhi[1]++;
        }
        else
        {
            city[i].belong=0;
            guanzhi[0]++;
        }
    }

    sort(city,city+N,cmp);

    int pos=0;
    if(fabs(guanzhi[0]-guanzhi[1])>C)
    {
        if(guanzhi[0]>guanzhi[1])
        {
            while((guanzhi[0]-guanzhi[1])>C)
            {
                int t=city[pos].belong;
                if(t==1)
                {
                    pos++; continue;
                }
                guanzhi[t]--; guanzhi[1^t]++;
                city[pos].belong=1^t;
                pos++;
            }
        }
        else if(guanzhi[0]<guanzhi[1])
        {
            while((guanzhi[1]-guanzhi[0])>C)
            {
                int t=city[pos].belong;
                if(t==0)
                {
                    pos++; continue;
                }
                guanzhi[t]--; guanzhi[1^t]++;
                city[pos].belong=1^t;
                pos++;
            }
        }
    }

    sort(city,city+N,cmp2);

    for(int i=0;i<N;i++)
    {
        cout<<city[i].belong+1<<" ";
    }
    cout<<endl<<endl;

}
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值