HDU 4885 TIANKENG’s travel 最短路

判断是否共线用map记录下斜率;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include<algorithm>
#include<math.h>
#include<map>
using namespace std;
#define N 1022
const int INF = 1<<30-1;
bool vis[2020];
int mat[1022][1022],lowcost[1022],pre[1033];
double cost[1022][1022];
int maxlong,star,end;
double mathh(int a,int c,int b,int d)
{
    return sqrt((a-b)*(a-b)*1.0+(c-d)*(c-d)*1.0);
}
double xielv(int a,int c,int b,int d)
{
    if(a==b)//垂直x轴
        return INF;
    else return (d-c)*1.0/(b-a);
}
struct node
{
    int x,y;
} point[1022];
bool cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
struct node1
{
    int star,step;
};
void Dijkstra(int n,int beg)
{
    for(int i=0; i<n; i++)
    {
        lowcost[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    lowcost[beg]=0;
    for(int j=0; j<n; j++)
    {
        int k=-1;
        int Min=INF;
        for(int i=0; i<n; i++)
            if(!vis[i]&&lowcost[i]<Min)
            {
                Min=lowcost[i];
                k=i;
            }
        if(k==-1)break;
        vis[k]=true;
        for(int i=0; i<n; i++)
            if(!vis[i]&&lowcost[k]+mat[k][i]<lowcost[i])
            {
                lowcost[i]=lowcost[k]+mat[k][i];
                pre[i]=k;
            }
    }
}
int main()
{
    int n,t;
    // freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        memset(mat,0,sizeof(mat));
        memset(cost,0,sizeof(cost));
        scanf("%d%d",&n,&maxlong);
        for(int i=0; i<n+2; i++)
            scanf("%d%d",&point[i].x,&point[i].y);
        int a=point[1].x,b=point[1].y;
        int c=point[0].x,d=point[0].y;
        sort(point,point+n+2,cmp);
        for(int i=0; i<n+2; i++) //找到起点和终点
        {
            if(a==point[i].x&&b==point[i].y)
                end=i;
            if(c==point[i].x&&d==point[i].y)
                star=i;
        }
        for(int i=0; i<n+2; i++)//计算距离
            for(int j=0 ; j<n+2; j++)
                cost[i][j]=mathh(point[i].x,point[i].y,point[j].x,point[j].y);
        for(int i=0; i<n+2; i++)
        {
            map<double ,int > m;
            m.clear();
            for(int j=i+1; j<n+2; j++)//i点开始与j点的斜率
            {
                if(cost[i][j]<=maxlong)//先判断距离
                {
                    double xie=xielv(point[i].x,point[i].y,point[j].x,point[j].y);
                    if(m.find(xie)==m.end())//斜率先前没出现过
                    {
                        mat[i][j]=mat[j][i]=1;
                        m[xie]=1;
                    }
                    else
                        mat[i][j]=mat[j][i]=INF;
                }
                else
                    mat[i][j]=mat[j][i]=INF;
            }
        }
        Dijkstra(n+2,star);//最短路
        if(lowcost[end]==INF)//没有走到终点
            printf("impossible\n");
        else printf("%d\n",lowcost[end]-1);//走到终点的那一步不是加油站要-1;
    }
    return 0;
}

HDU 4885 TIANKENG’s travel 最短路

时间: 2024-11-09 02:27:28

HDU 4885 TIANKENG’s travel 最短路的相关文章

hdu 4885 TIANKENG’s travel (最短路+判断三点共线)

TIANKENG's travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 408    Accepted Submission(s): 100 Problem Description TIANKENG has get a driving license and one day he is so lucky to find a

hdu 4885 TIANKENG’s travel(bfs)

题目链接:hdu 4885 TIANKENG's travel 题目大意:给定N,L,表示有N个加油站,每次加满油能够移动距离L,必须走直线,可是能够为斜线.然后给出sx,sy,ex,ey,以及N个加油站的位置,问说最少经过几个加油站,路过不加油也算. 解题思路:一開始以为经过能够不算,所以o(n2)的复杂度建图,然后用bfs求最短距离,结果被FST了. 将点依照x坐标排序,这样在建图是保证当前点为最左点,每次建立一条边的时候,将该边的斜率记录,下次有同样的斜率则不加边,斜率能够用两个整数表示,

[ACM] HDU 4885 TIANKENG’s travel (特殊建图,最短路)

TIANKENG's travel 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 hom

hdu 4885 (n^2*log(n)判断三点共线建图)+最短路

题意:车从起点出发,每次只能行驶L长度,必需加油到满,每次只能去加油站或目的地方向,路过加油站就必需进去加油,问最小要路过几次加油站. 开始时候直接建图,在范围内就有边1.跑最短了,再读题后发现,若几个点共线,且都在范围内,那么中间有点的俩头的点就不能有边,否则与条件相悖.关键是怎么用n^2*logn,的复杂度判断三点共线:点先按X排序,考察每个点i时候,第二个点j,若直线ij斜率已经存在,则不能添加了,查找是否存在,用容器就行(map\set)都是logn的,所以满足要求.之后最短路即可. #

hdu 4883 TIANKENG’s restaurant(暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come t

HDU 4883 TIANKENG’s restaurant Bestcoder 2-1(模拟)

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of t

HDU 4883 TIANKENG’s restaurant(区间选点)

HDU 4883 TIANKENG's restaurant 题目链接 题意:给定一些时间作为区间,和一个人数,问要安排多少人去看管(一个人只能看管一个人) 思路:普通的区间选点问题,一个区间拆成一个进入点一个出去点,然后排序循环求答案即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 20005; struct Man {

HDU 4034 Graph(floyd,最短路,简单)

题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXN=110; const int INF=0x3f3f3f3f;//防止后面溢出,这个不能太大 bool vis[MAXN]; int pr

HDU 4883 TIANKENG’s restaurant (贪心)

链接:带我学习,带我飞 第一次BC,稳挂,WA n多次,今天重新做了一下 略挫 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <string> #include <vector> #include <set> #include <algorithm>