题目描述:

In this problem we consider a very simplified model of Barcelona city.

Barcelona can be represented as a plane with streets of kind x=cx=c and y=cy=c for every integer cc (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x,y)(x,y) for which ax+by+c=0ax+by+c=0.

One can walk along streets, including the avenue. You are given two integer points AAand BB somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to BB from AA.

Input

The first line contains three integers aa, bb and cc (−109≤a,b,c≤109−109≤a,b,c≤109, at least one of aa and bb is not zero) representing the Diagonal Avenue.

The next line contains four integers x1x1, y1y1, x2x2 and y2y2 (−109≤x1,y1,x2,y2≤109−109≤x1,y1,x2,y2≤109) denoting the points A=(x1,y1)A=(x1,y1) and B=(x2,y2)B=(x2,y2).

Output

Find the minimum possible travel distance between AA and BB. Your answer is considered correct if its absolute or relative error does not exceed 10−610−6.

Formally, let your answer be aa, and the jury's answer be bb. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6|a−b|max(1,|b|)≤10−6.

Examples

Input

1 1 -3
    0 3 3 0

Output

4.2426406871

Input

3 1 -9
    0 3 3 -1

Output

6.1622776602

思路:

刚开始,凭感觉做的是通过画图,像是从A点出发直着向上/向下到达直线,从A点出发直着向右/向左到达直线,与从B出发直着向上/向下到达直线,从B出发直着向右/向左到达直线,共四种组合的距离,加上曼哈顿距离,求最小距离

然后,想的是从A点开始,到与直线相交,的每一种情况下,B也同样处理得到距离的最小值,说的不太清楚,如图

对每个A来说,算出每个B路径下的总的距离,取最小。

这里面有几个路径在程序中没包括,就是上图的紫色和黄色,组合在一起的情况,不过这样算会最终超时

小心的是数据用long long,不然计算中会溢出,还有输出格式的设置,想要设置成浮点数(不用科学计数法),设置精度等等

知识点:暴力

代码:

(忽略dd函数,那是会查实的,程序实际调用的是dd1函数)

 #include <iostream>
#include <cmath>
#include <iomanip>
#include <climits>
using namespace std;
long long a,b,c;
long long x1,y1;
long long x2,y2;
double dd1(long long x1,long long y1,long long x2,long long y2)
{
double dist1 = ;
double yy1 = (-a*x1-c)/(double)b;
dist1 += abs(y1-yy1);
double yy2 = (-a*x2-c)/(double)b;
dist1 += abs(y2-yy2);
dist1 += sqrt((x1-x2)*(x1-x2)+(yy1-yy2)*(yy1-yy2));
double dist2 = ;
double xx2 = (-b*y2-c)/(double)a;
dist2 += abs(x2-xx2);
dist2 += abs(y1-yy1);
dist2 += sqrt((x1-xx2)*(x1-xx2)+(yy1-y2)*(yy1-y2));
double dist3 = ;
double xx1 = (-b*y1-c)/(double)a;
dist3 += abs(xx1-x1);
dist3 += abs(x2-xx2);
dist3 += sqrt((xx1-xx2)*(xx1-xx2)+(y1-y2)*(y1-y2));
double dist4 = ;
dist4 += abs(xx1-x1);
dist4 += abs(y2-yy2);
dist4 += sqrt((xx1-x2)*(xx1-x2)+(y1-yy2)*(y1-yy2));
double dist = dist1;
dist = min(dist,dist2);
dist = min(dist,dist3);
dist = min(dist,dist4);
return dist;
}
double dd(int x1,int x2,int x3,int x4)
{
double mdist = INT_MAX;
double xx1 = (-b*y1-c)/(double)a;
double xx2 = (-b*y2-c)/(double)a;
int length = abs(x1-(int)xx1);
for(int i = ;i<=length;i++)
{
double dist = ;
int x;
if(a*x1+b*x2+c<)
{
x = x1+i;
}
else
{
x = x1-i;
}
dist += i;
double yy1 = (-a*x-c)/(double)b;
dist += abs(y1-yy1);
int length2 = abs(x2-(int)xx2);
for(int j = ;j<=length2;j++)
{
if(a*x2+b*x2+c<)
{
x = x2+j;
}
else
{
x = x2-j;
}
dist += j;
double yy2 = (-a*x-c)/(double)b;
dist += abs(y2-yy2);
dist += sqrt((x2-x1)*(x2-x1)+(yy1-yy2)*(yy1-yy2));
if(dist<mdist)
{
mdist = dist;
}
} }
double dist = dd1(x1,x2,y1,y2);
if(dist<mdist)
{
mdist = dist;
}
return mdist;
}
int main()
{
cin >> a >> b >> c;
cin >> x1 >> y1 >> x2 >> y2;
double dist = abs(x1-x2)+abs(y1-y2);
if(a!=&&b!=)
{
double ans = dd1(x1,y1,x2,y2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << setprecision() << min(dist,ans) << endl;
}
else
{
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << dist << endl;
}
return ;
}

最新文章

  1. 浅谈Service层为何要有接口
  2. 模拟Post请求
  3. cefsharp开发实例1
  4. GCD之dispatch queue深入浅出
  5. mysql server has gone away 与max_allowed_packed
  6. VBA Excel 常用 自定义函数
  7. Java高级--Java线程运行栈信息的获取 getStackTrace()
  8. Ubuntu14.04右键菜单添加Sublime 2打开选项
  9. 互联网创业应该如何找到创意 - RethinkDB创始人Slava Akhmechet的几点建议
  10. Strange fuction
  11. 听翁恺老师mooc笔记(11)--结构和函数
  12. Codechef August Challenge 2018 : Coordinate Compression
  13. SSH整合时多表关联查询出现Javassist增强失败
  14. 2018-2019-2 网络对抗技术 20165305 Exp2 后门原理与实践
  15. redis安装,windows,linux版本并部署服务
  16. Java HashMap的put操作(Java1.6)
  17. 51Nod 部分题目 の 口胡&amp;一句话题解
  18. 《剑指offer》第五十题(字符流中第一个只出现一次的字符)
  19. stop 用法
  20. 学习5_STM32--外设通信方式

热门文章

  1. mysql:获取某个表的所有字段
  2. thinkphp5 模板url标签 跟javascript ajax 的 url 参数 被莫名替换
  3. MySQL多表查询,Navicat使用,pymysql模块,sql注入问题
  4. LeetCode 5073. 进击的骑士(Java)BFS
  5. Docker之网络配置
  6. K8s-yaml的使用及命令
  7. golang 之 go-micro
  8. 仅反射加载(ReflectionOnlyLoadFrom)的 .NET 程序集,如何反射获取它的 Attribute 元数据呢?
  9. C# vb .net实现焦距淡色特效滤镜
  10. 2019 蚂蚁金服java面试笔试题 (含面试题解析)