BC#2 hdu4885

TIANKENG’s travel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 413    Accepted Submission(s): 103


Problem Description
TIANKENG has get a driving license and one day he is so lucky to find a car. Every day he drives the car around the city. After a month TIANKENG finds that he has got high driving skills and he wants to drive home. Assuming that we regard the map of Hangzhou as a two-dimensional Cartesian coordinate system, the coordinate of the school is (sx, sy), the coordinate of TIANKENG’s home is (ex,ey). There are n gas stations distributing along the road and the coordinate of the ith gas station is (xi, yi). Because there is something wrong with the car engine, TIANKENG can drive the car for L miles after every time he fills up the gas. Assuming that TIANKENG must fill up the gas when he passes by a gas station and the initial status of the tank is full. TIANKENG must drive on the straight line and he can only choose to drive to school or home or gas station. Your task is to tell TIANKENG the minimum times he needs to charge fuel to go home successfully.
 

Input
The first line contains a positive integer T(T<=30), referring to T test cases.
For each test case, the first line contains two integers n(0<=n<=1000), L(1<=L<=100000), which mean n gas stations in all and the distance TIANKENG can drive with the full tank respectively. The second line contains two integers sx, sy, which refers to the coordinate of the school.
The third line contains two integers ex, ey, which refers to the coordinate of TIANKENG’s home.
Then following n lines, each line contains two integer xi, yi, which refers to the coordinate of gas station.

Assuming that all the coordinates are different! All the coordinates satisfied [0,1000000].
 

Output
For each test case, if TIANKENG cannot go home successfully, print “impossible”(without quotes), otherwise print the minimum times of charging fuel.
 

Sample Input
2 2 1000 0 0 2000 1000 1000 0 2000 0 0 1000 0 0 2000 1000
 

Sample Output
2 impossible
 

Source
 

 

开始以为是直接O(n^2)建图 跑bfs。由于两个点之间可能存在点这个点也必须算经过 , 题解说要极角排序,看有人用斜率来标记做 ,就尝试了一下 ,先对点按x排序然后在n^2枚举每对点 , 加边的条件是 当前该斜率未出现过且距离满足题目所给的限制 在最后跑bfs ok!

还是联系java,果然是很捉急!

对于自定义类的set必须要对该类添加接口 Comparable 并 重写 compareTo方法

import java.util.*;
import java.math.*;

public class Main {
    final int N=1011;
    Set<node> set=new TreeSet<node>();
    Vector<Integer> g[]=new Vector[N];
    point p[]=new point[N];
    long L;
    boolean vis[]=new boolean[N];
    int d[]=new int[N];
    void init(){
        for(int i=0;i<N;i++)p[i]=new point();
        for(int i=0;i<N;i++)g[i]=new Vector<Integer>();
    }
    long dis(int i,int j){
        long x=p[i].x-p[j].x;
        long y=p[i].y-p[j].y;
        return x*x+y*y;
    }
    void addedge(int i,int j){
        node x=new node(p[i].x-p[j].x,p[i].y-p[j].y);
        //System.out.println(set.contains(x));
        
        if(dis(i,j)>L*L||set.contains(x))return;
        int u=p[i].id,v=p[j].id;
        g[u].add(v);
        g[v].add(u);
        //System.out.println(x.x+"  "+x.y);
        //System.out.println(u+" -> "+v);
        
        set.add(x);
    }
    void bfs(int s,int t){
        Queue<Integer> q=new LinkedList<Integer>();
        q.offer(s);
        Arrays.fill(vis, false);
        Arrays.fill(d,-1);
        vis[s]=true;
        d[s]=0;
        while(!q.isEmpty()){
            int u=q.poll();
            int sz=g[u].size();
            for(int i=0;i<sz;i++){
                int v=g[u].get(i);
                if(!vis[v]){
                    d[v]=d[u]+1;
                    q.offer(v);
                    vis[v]=true;
                }
            }
        }
        //for(int i=0;i<4;i++)
        if(d[t]==-1)System.out.println("impossible");
        else
            System.out.println(d[t]-1);
    }
    void work(){
        set.clear();
        init();
        int T,n;
        Scanner cin=new Scanner (System.in);
        T=cin.nextInt();
        while((T--)!=0){
            n=cin.nextInt();L=cin.nextLong();
            n+=2;
            for(int i=0;i<n;i++)g[i].clear();
            for(int i=0;i<n;i++){
                p[i].x=cin.nextInt();
                p[i].y=cin.nextInt();
                p[i].id=i;
            }
            Arrays.sort(p,0 ,n);
            //for(int i=0;i<n;i++)p[i].out();
            for(int i=0;i<n;i++){
                set.clear();
                for(int j=i+1;j<n;j++){
                    addedge(i,j);
                }
            }
            bfs(0,1);
        }
    }
    public static void main(String []args){
        new Main().work();
    }
}
class point implements Comparable{
    int x,y,id;
    point(){}
    point(int x,int y,int id){
        this.x=x;this.y=y;this.id=id;
    }
    void out(){
        System.out.println(x+" ,"+ y+" ,"+id);
    }
    
    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        point a0=(point)o;
        if(x>a0.x||a0.x==x&&a0.y<y)return 1;
        return -1;
    }
}
/*class Comp implements Comparator{

    @Override
    public int compare(Object arg0, Object arg1) {
        // TODO Auto-generated method stub
        point a0=(point)arg0;point a1=(point)arg1;
        if(a0.x>a1.x||a0.x==a1.x&&a0.y>a1.y)return 1;
        return -1;
    }
    
}*/

class node implements Comparable{
    int x,y;
    node(){}
    node(int xx,int yy){
        if(xx==0){
            y=1;x=0;
        }
        else if(yy==0){
            x=1;y=0;
        }
        else{
            int g=gcd(Math.abs(xx),Math.abs(yy));
            x=xx/g;
            y=yy/g;
            if(x<0){
                x=-x;
                y=-y;
            }
        }
    }
    int gcd(int a,int b){
        return b==0?a:gcd(b,a%b);
    }
    
    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        node z=(node) arg0;
        if(z.x>x||z.x==x&&z.y>y)return 1;
        else if(z.x==x&&z.y==y)return 0;
        
        else return -1;
    }
}
View Code

 

posted @ 2014-07-30 15:38  Mr.Youyu  阅读(161)  评论(0)    收藏  举报