Codeforces I. Barcelonian Distance
Posted zhanhonhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces I. Barcelonian Distance相关的知识,希望对你有一定的参考价值。
题目描述:
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函数)
1 #include <iostream> 2 #include <cmath> 3 #include <iomanip> 4 #include <climits> 5 using namespace std; 6 long long a,b,c; 7 long long x1,y1; 8 long long x2,y2; 9 double dd1(long long x1,long long y1,long long x2,long long y2) 10 11 double dist1 = 0; 12 double yy1 = (-a*x1-c)/(double)b; 13 dist1 += abs(y1-yy1); 14 double yy2 = (-a*x2-c)/(double)b; 15 dist1 += abs(y2-yy2); 16 dist1 += sqrt((x1-x2)*(x1-x2)+(yy1-yy2)*(yy1-yy2)); 17 double dist2 = 0; 18 double xx2 = (-b*y2-c)/(double)a; 19 dist2 += abs(x2-xx2); 20 dist2 += abs(y1-yy1); 21 dist2 += sqrt((x1-xx2)*(x1-xx2)+(yy1-y2)*(yy1-y2)); 22 double dist3 = 0; 23 double xx1 = (-b*y1-c)/(double)a; 24 dist3 += abs(xx1-x1); 25 dist3 += abs(x2-xx2); 26 dist3 += sqrt((xx1-xx2)*(xx1-xx2)+(y1-y2)*(y1-y2)); 27 double dist4 = 0; 28 dist4 += abs(xx1-x1); 29 dist4 += abs(y2-yy2); 30 dist4 += sqrt((xx1-x2)*(xx1-x2)+(y1-yy2)*(y1-yy2)); 31 double dist = dist1; 32 dist = min(dist,dist2); 33 dist = min(dist,dist3); 34 dist = min(dist,dist4); 35 return dist; 36 37 double dd(int x1,int x2,int x3,int x4) 38 39 double mdist = INT_MAX; 40 double xx1 = (-b*y1-c)/(double)a; 41 double xx2 = (-b*y2-c)/(double)a; 42 int length = abs(x1-(int)xx1); 43 for(int i = 0;i<=length;i++) 44 45 double dist = 0; 46 int x; 47 if(a*x1+b*x2+c<0) 48 49 x = x1+i; 50 51 else 52 53 x = x1-i; 54 55 dist += i; 56 double yy1 = (-a*x-c)/(double)b; 57 dist += abs(y1-yy1); 58 int length2 = abs(x2-(int)xx2); 59 for(int j = 0;j<=length2;j++) 60 61 if(a*x2+b*x2+c<0) 62 63 x = x2+j; 64 65 else 66 67 x = x2-j; 68 69 dist += j; 70 double yy2 = (-a*x-c)/(double)b; 71 dist += abs(y2-yy2); 72 dist += sqrt((x2-x1)*(x2-x1)+(yy1-yy2)*(yy1-yy2)); 73 if(dist<mdist) 74 75 mdist = dist; 76 77 78 79 80 double dist = dd1(x1,x2,y1,y2); 81 if(dist<mdist) 82 83 mdist = dist; 84 85 return mdist; 86 87 int main() 88 89 cin >> a >> b >> c; 90 cin >> x1 >> y1 >> x2 >> y2; 91 double dist = abs(x1-x2)+abs(y1-y2); 92 if(a!=0&&b!=0) 93 94 double ans = dd1(x1,y1,x2,y2); 95 cout.setf(ios_base::fixed,ios_base::floatfield); 96 cout << setprecision(11) << min(dist,ans) << endl; 97 98 else 99 100 cout.setf(ios_base::fixed,ios_base::floatfield); 101 cout << dist << endl; 102 103 return 0; 104
以上是关于Codeforces I. Barcelonian Distance的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces I. Producing Snow(优先队列)
[CF1032D] Barcelonian Distance - 计算几何
[CF1032D] Barcelonian Distance - 计算几何