TOJ1017: Tour Guide

 

描述

You are working as a guide on a tour bus for retired people, and today you have taken your regular Nordic seniors to The Gate of Heavenly Peace. You let them have a lunch break where they could do whatever they like. Now you have to get them back to the bus, but they are all walking in random directions. You try to intersect them, and send them straight back to the bus. Minimize the time before the last person is in the bus. You will always be able to run faster than any of the tour guests, and they walk with constant speed, no matter what you tell them. The seniors walk in straight lines, and the only way of changing their direction is to give them promises of camphor candy. A senior will neither stop at nor enter the bus before given such a promise.

输入

A number of test cases consisting of: A line with an integer 1 ≤ n ≤ 8, the number of people on the tour. A line with an floating point number 1 < v ≤ 100, your maximum speed (you start in the bus at the origin). Then follow n lines, each containing four floating point numbers xi yi vi ai, the starting coordinates (−106 ≤ xi, yi ≤ 106), speed (1 ≤ vi < 100) and direction (0 ≤ ai < 2π) of each of the tour guests. 

The input is terminated by a case with n = 0, which should not be processed. All floating point numbers in the input will be written in standard decimal notation, and have no more than 10 digits.

输出

For each test case, print a line with the time it takes before everybody is back in the bus (the origin). Round the answer to the nearest integer. The answer will never be larger than 106.

样例输入

1
50.0
125.0 175.0 25.0 1.96
3
100.0
40.0 25.0 20.0 5.95
-185.0 195.0 6.0 2.35
30.0 -80.0 23.0 2.76
0

样例输出

20
51

提示

Q: Should 2.5000000000 be rounded to 3? 
A: We have constructed the test cases such that it does not matter.

题目来源

Nordic 2006

这个题目最简洁的想法就是去枚举接哪些人的,也就是n!的写法,当时学长亲测是可以过的,而且代码也稍微短点

我dfs进行剪枝了,所以稍微快点吧

枚举所有情况,然后根据距离公式求得当前的时间

 

#include<bits/stdc++.h>
using namespace std;
struct Node
{
    double x,y,v,d;
} p[8];
int id[8],N;
double mi;
double lb(Node g, Node s, Node *mg)
{
    /*(s.x−g.x+vcd*t)*(s.x−g.x+vcd*t)+(s.y−g.y+vsd*t)*(s.y−g.y+vsd*t)=g.v*(t-g.d)*g.v*(t-g.d)
    两者之间的距离公式
    a=vcd*vcd+vsd*vsd-g.v*g.v=s.v*s.v*(sin(s.d)*sin(s.d)+cos(s.d)*cos(s.d))-g.v*g.v=s.v*s.v*-g.v*g.v;
    b=2*((g.v*g.v*g.d)+(s.x-g.x)*vcd+(s.y-g.y)*vsd)
    c=(s.x-g.x)*(s.x-g.x)+(s.y-g.y)*(s.y-g.y)-(g.v*g.d)*(g.v*g.d)*/
    double c0,c1,c2,vsd,vcd,a,b,c,x,y,t;
    vsd=s.v*sin(s.d);//y轴的分速度
    vcd=s.v*cos(s.d);//x轴的分速度
    c0=g.v*g.v*g.d;
    c1=s.x-g.x;//两点的横坐标之差
    c2=s.y-g.y;//两点的纵坐标之差
    a=s.v*s.v-g.v*g.v;
    b=2*(c0+c1*vcd+c2*vsd);
    c=c1*c1+c2*c2-c0*g.d;
    t=-(b+sqrt(b*b-4*a*c))/(2*a);//求其正整数解
    x=s.x+vcd*t;//也就是老人的速度加上他又走的
    y=s.y+vsd*t;//也就是老人的速度加上他又走的
    mg->x=x;//求得现在的位置x
    mg->y=y;//求得现在的位置y
    mg->v=g.v;//求得导游的速度
    mg->d=t;//求得时间t
    return t+sqrt(x*x+y*y)/s.v;//求得时间+老人走回来的时间
}
double la(Node g, double tt, int deep)
{
    double t;
    if(deep==0) mi=1e7;
    if(deep==N&&mi>tt) mi=tt;
    for(int i=deep; i<N; i++)
    {
        swap(id[i],id[deep]);
        Node mg;
        t=lb(g,p[id[deep]],&mg);//求得当前的时间
        if(t<mi) la(mg,t>tt?t:tt,deep+1);//比最小时间小,说明当前时间合法,可以继续递归
        swap(id[i],id[deep]);//重新交换回来检查是不是还能更小
    }
    return mi;
}
int main()
{
    for(int i = 0; i<8;i++)id[i]=i;
    while(scanf("%d",&N),N)
    {
        double v;
        scanf("%lf",&v);
        for(int i=0; i<N; i++)
            scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].v,&p[i].d);
        Node g= {0,0,v,0};
        printf("%.0f
", la(g,0,0));
    }
    return 0;
}

 

原文地址:https://www.cnblogs.com/BobHuang/p/8673032.html