csu2088--- Pigs can't take a sudden turn解题报告(二元一次函数的最小值---计算几何)

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/83692589

                  2088: Pigs can't take a sudden turn题目链接

题意:求两个随速度变化的点的最小距离(x1 + u1 * t, y1+ v1 * t),(x2 + u2 * t, y2 + v1 * t)。

思路:化简两点间的距离公式得到d^2关于t的二元一次方程:

d ^ 2 = ((u2 - u1)^ 2 + (v2 - v1) ^ 2) * t^2 + ((x2 - x1) * (u2 - u1) + (y2 - y1) * (v2 - v1))2t + (x2 - x1)^2 + (y2 - y1) ^ 2。由此得到二元方程的a, b, c。

(y1好像是csu系统里面定义过, 所以#define重新定义一下)

AC Code:

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 1000000007
#define INF 0x3f3f3f3f
#define y1 AC
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i = a; i < b; i++)
using namespace std;
const double pi = acos(-1);
const int MAX_N = 1e5 + 5;
const double eps = 1e-8;
typedef long long ll;
double a, b, c;
double x1, y1, u1, v1;
double x2, y2, u2, v2;
double get_ans(double t){
    double ans = a * t * t + b * t + c;
    return sqrt(ans);
}
int main(){
    //freopen("c1.txt", "w", stdin);
    //freopen("c2.txt", "r", stdout);
    //ios::sync_with_stdio(false);
    int T, cas = 1;
    scanf("%d", &T);
    while(T--){
        scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
        scanf("%lf%lf%lf%lf", &u1, &v1, &u2, &v2);
        a = (u2 - u1) * (u2 - u1) + (v2 - v1) * (v2 - v1);
        b = 2 * ((x2 - x1) * (u2 - u1) + (y2 - y1) * (v2 - v1));
        c = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
        printf("Case %d: ", cas++);
        if(fabs(a) < eps){
            printf("%.6lf\n", get_ans(0));
        }
        else if(-b / (2 *a) < 0){
            printf("%.6lf\n", get_ans(0));
        }
        else{
            printf("%.6lf\n", get_ans(-b / (a * 2)));
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/83692589