HDU4766——Network

Problem

With the help of the Ares L, the Emperor of Law and some stars of the future, General M succeeded in setting foot on the mountain, and had set up a kingdom, known as ACMSYSU. In order to reward these founding heroes, General M decided to share her network.
General M has manufactured a router. One can use this network if the Euclidean distance between his/her house and the router is not greater than d. Because of General M's excellent technology, the distance between her house and the router can be infinite.
There are n heroes in the kingdom besides General M. General M is not an eccentric person, so she hopes everyone can use the network. Because the farther the router is away from her house, the worse the signal, General M wants the router being as close to her house as possible. Please help General M place the router so that the distance between her house and the router is minimized, and every one can use the router. You just need to tell General M the minimum distance.

Input

The input will consist of multiple test cases.
For each case, the first line contains 3 integers x, y and d, which represent that the General M's house locates at the point (x, y) and one can use the network if his/her distance to the router is not greater than d.
The second line is a positive integer n, the number of heroes of the kingdom.
Then n lines follow. Each line has 2 integers x_i and y_i, indicating that the i-th hero locates at point (x_i, y_i)。
All integers in the input satisfy that their absolute values are less than or equal to 10^5, and 1<=n<=10^3.

Output

For each case, output a real number which indicates the minimum distance between the General M's house and the router, to two decimal places.
If it is impossible to share a network which can cover everyone, output an 'X' (without the single quote).

Sample Input

0 0 5
2
5 3
5 -3
0 0 1
2
5 3
5 -3

Sample Output

1.00
X


思路

分这样几种情况讨论:
①如果路由器恰好放在主人家的位置上即可覆盖到所有英雄,那么答案就是0.00;
②如果以路由器为圆心的圆周上只有一个点,则圆心在连线上;
③如果以路由器为圆心的圆周上有两个或多个点,则当两个点或者更多的点都距离为d时确定圆心;
复杂度:O(n³),是的没错,这个复杂度也可以过……这点又给学长指出来了,所以以后有复杂度更优的做法再补。

代码一

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const double INF = 0x3f3f3f3f;
const int M = 1005;
int n;
double x, y, d;
struct Point {
    double x, y;
    Point() {}
    Point(double _x, double _y) :x(_x), y(_y) {}
    Point operator+(Point p) { return Point(x + p.x, y + p.y); }
    Point operator-(Point p) { return Point(x - p.x, y - p.y); }
    Point operator*(double p) { return Point(p*x, p*y); }
    Point operator/(double p) { return Point(x / p, y / p); }
    Point operator+=(Point p) { *this = *this + p; return *this; }
    Point operator-=(Point p) { *this = *this - p; return *this; }
    Point operator*=(double p) { *this = *this * p; return *this; }
    Point operator/=(double p) { *this = *this / p; return *this; }
    bool operator==(Point p) { return abs(x - p.x)<EPS&&abs(y - p.y)<EPS; }
    bool operator!=(Point p) { return !(*this == p); }
    double dot(Point p) { return x*p.x + y*p.y; }
    double det(Point p) { return x*p.y - y*p.x; }
    double length() { return sqrt(x*x + y*y); }
    Point rotate(double rad) { return Point(x*cos(rad) - y*sin(rad), x*sin(rad) + y*cos(rad)); }
}arr[M];
bool judge(Point p) {
    for (int i = 1; i <= n; i++) {
        Point t = arr[i] - p;
        if (t.length()>d&&abs(t.length() - d)>EPS)return false;
    }
    return true;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(2);
    while (cin >> x >> y >> d) {
        memset(arr, 0, sizeof(arr));
        arr[0] = Point(x, y);
        cin >> n;
        for (int i = 1; i <= n; i++)cin >> arr[i].x >> arr[i].y;
        if (judge(arr[0])) { cout << "0.00" << endl; continue; }
        double min_value = INF;
        for (int i = 1; i <= n; i++) {
            Point t = arr[0] - arr[i];
            t *= (d / t.length()); 
            t += arr[i];
            Point t0 = t - arr[0];
            if (t0.length()<min_value&&judge(t))min_value = t0.length();
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                Point t = (arr[i] + arr[j]) / 2.0;
                Point t0 = arr[i] - arr[j];
                double dis = 0.5*t0.length();
                Point vec1 = t0.rotate(PI / 2.0)*sqrt(d*d - dis*dis) / (dis*2.0);
                Point vec2 = t0.rotate(-PI / 2.0)*sqrt(d*d - dis*dis) / (dis*2.0);
                Point t1 = t + vec1, t2 = t + vec2;
                Point t3 = t1 - arr[0], t4 = t2 - arr[0];
                if (t3.length()<min_value&&judge(t1))min_value = t3.length();
                if (t4.length()<min_value&&judge(t2))min_value = t4.length();
            }
        }
        if (abs(min_value - INF)<EPS)cout << "X" << endl;
        else cout << min_value << endl;
    }
    return 0;
}

代码二

学长的二分AC代码先贴这里,速度确实快了不少,然而在我自己电脑上跑不出正确结果但是hdu可以AC?先马了以后看~~

#include<cstdio>
#include<utility>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1005;
const double pi = acos(-1.0);
const double eps = 1e-6;
pair<double, double> fr[maxn], center;
double d;
int n;
pair<double, double> operator+(const pair<double, double> a, const pair<double, double> b) {
    return pair<double, double>(a.first + b.first, a.second + b.second);
}
pair<double, double> operator-(const pair<double, double> a, const pair<double, double> b) {
    return pair<double, double>(a.first - b.first, a.second - b.second);
}
pair<double, double> operator*(const pair<double, double> a, const double s) {
    return pair<double, double>(a.first*s, a.second*s);
}
pair<double, double> operator/(const pair<double, double> a, const double s) {
    return pair<double, double>(a.first / s, a.second / s);
}
double norm(pair<double, double> x) {
    return sqrt(x.first*x.first + x.second*x.second);
}
pair<double, double> rotate(const pair<double, double> &p, double cost, double sint) {
    return pair<double, double>(p.first*cost - p.second*sint, p.first*sint + p.second*cost);
}
pair<pair<double, double>, pair<double, double>> crosspoint(pair<double, double> ap, double ar, pair<double, double> bp, double br) {
    double d = norm(ap - bp);
    double cost = (ar*ar + d*d - br*br) / (2 * ar*d);
    double sint = sqrt(1.0 - cost*cost);
    pair<double, double> v = (bp - ap) / norm(bp - ap)*ar;
    return make_pair(ap + rotate(v, cost, -sint), ap + rotate(v, cost, sint));
}
int sgn(double x) {
    if (x<-eps)return -1;
    return x>eps;
}
bool ifinclude(pair<double, double> a, double ar, pair<double, double> b, double br) {
    return sgn(norm(a - b) - (ar + br))<0 && sgn(norm(a - b) - abs(ar - br))>0;
}
bool check(double r) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (ifinclude(pair<double, double>(0, 0), r, fr[i], d)) {
            cnt++;
            auto cpt = crosspoint(pair<double, double>(0, 0), r, fr[i], d);
            bool flg = true;
            for (int i = 1; i <= n; i++)
                if (sgn(norm(cpt.first - fr[i]) - d)>0) { flg = false; break; }
            if (flg)return true;
            flg = true;
            for (int i = 1; i <= n; i++)
                if (sgn(norm(cpt.second - fr[i]) - d)>0) { flg = false; break; }
            if (flg)return true;
        }
    }
    return cnt == 0;
}
bool sol() {
    int cx, cy, cd;
    if (!(~scanf("%d%d%d", &cx, &cy, &cd)))return false;
    d = cd;
    double l = 0, r = 1e10;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int tx, ty;
        scanf("%d%d", &tx, &ty);
        fr[i] = pair<double, double>(tx - cx, ty - cy);
        l = max(l, norm(fr[i]) - d);
        r = min(r, norm(fr[i]) + d);
    }
    if (sgn(l - r) >= 0) { puts("X"); return true; }
    bool flg = false;
    while (r - l>eps) {
        double mid = (l + r) / 2;
        if (check(mid))r = mid, flg = true;
        else l = mid;
    }
    if (flg)printf("%.2lf\n", (double)l);
    else printf("X\n");
    return true;
}
int main() {
    while (sol());
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容