Ural 1750. Pakhom and the Gully

1750. Pakhom and the Gully

Time Limit: 1.0 second
Memory Limit: 64 MB
Pakhom stands at the point S and wants to reach the point T to buy the land there. But he noticed a gully, which represents a polyline ABC. What is the length of the shortest path Pakhom should walk if he doesn't want to fall into the gully?
Problem illustration

Input

The first line contains the number of testcases n (1 ≤ n ≤ 5000). Each of the next n lines contains one testcase. Each testcase is written as 10 space-separated integers: xS, yS, xT, yT, xA, yA, xB, yB, xC, yC, the coordinates of the points S, T, A, B, and C, respectively. All points within the test case are different. Points S and T don't belong to the polyline ABC. All numbers in test cases don't exceed 10 by absolute value.

Output

For each test case output the answer on a separate line. The answer should be precise up to 10−6.

Sample

inputoutput
3
1 2 5 6 4 4 5 2 1 6
2 2 4 3 1 3 3 3 3 1
2 1 4 4 3 2 4 3 1 4
8.000000
3.650282
3.828427

 

表示被这道题目的 B点恶心的要死

 

本来想看百度题解的也没人写,  经过几天的挣扎, 再加上看discuss的数据,终于A了, 不容易啊

 

设起点为p[1], 终点为p[2], ABC点为p[3], p[4], p[5] ;

 

 

View Code
// ural 1750

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;

#define INF 100000
#define eps 1e-8

FILE *fout = fopen("output.txt", "w") ;
struct Point {
    double x, y;
} p[10];

double map[10][10] , q[10];

double dis(Point a, Point b){
    double x = a.x - b.x , y = a.y - b.y ;
    return sqrt(x * x + y * y) ;
}

double Multi(Point p0, Point p1, Point p2){
    return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x) ;
}

bool Cross(Point a, Point b, Point c, Point d){
    if(max(a.x, b.x) < min(c.x, d.x) || max(c.x, d.x) < min(a.x, b.x))    return 0 ;
    if(max(a.y, b.y) < min(c.y, d.y) || max(c.y, d.y) < min(a.y, b.y))    return 0 ;
    if( Multi(a, b, c) * Multi(a, b, d) <= eps &&
        Multi(c, d, a) * Multi(c, d, b) <= eps)    return 1;
    return 0;
}

void Disktra(){
    int visit[10] = {0} ;
    for(int i=1; i<=5; i++)    q[i] = map[1][i] ;

/*    for(int i=1; i<=5; i++)
        printf("%.2lf\n",q[i]) ;
*/
    int node ;
    visit[1] = 1;
    for(int i=2; i<=5; i++){
        double MIN = INF ;
        for(int j=2; j<=5; j++)
            if(!visit[j] && MIN > q[j])
                MIN = q[node = j] ;
        visit[node] = 1;
        for(int j = 2; j<=5; j++)
            if(!visit[j] && q[j] > q[node] + map[node][j])
                q[j] = map[node][j] + q[node] ;
    }
}

int main(){
    int T;
    scanf("%d", &T) ;
    while(T--){
        for(int i=1; i<=5; i++)
            scanf("%lf %lf", &p[i].x, &p[i].y) ;

        for(int i=1; i<=5; i++)
            for(int j=1; j<=5; j++)
                map[i][j] = INF ;
        for(int i=1; i<=5; i++)
            for(int j=i+1; j<=5; j++)
                map[i][j] = map[j][i] = dis(p[i], p[j]) ;

        if((Cross(p[1], p[2], p[3], p[4]) && fabs(Multi(p[1], p[2], p[3])) > eps)  //   线段1,2 如果和 34 45相交,  map[1][2] = INF ; 
        || (Cross(p[1], p[2], p[4], p[5]) && fabs(Multi(p[1], p[2], p[5])) > eps)){
            map[1][2] = map[2][1] = INF ;
            if(Multi(p[4], p[3], p[1]) * Multi(p[4], p[5], p[1]) < 0)      
                map[1][4] = map[4][1] = INF ;
            if(Multi(p[4], p[3], p[2]) * Multi(p[4], p[5], p[2]) < 0)     
                map[2][4] = map[4][2] = INF ;
        }

        if(Cross(p[1], p[3], p[4], p[5]) && fabs(Multi(p[1], p[3], p[5])) > eps)
            map[1][3] = map[3][1] = INF ;
        if(Cross(p[2], p[3], p[4], p[5]) && fabs(Multi(p[2], p[3], p[5])) > eps)
            map[2][3] = map[3][2] = INF ;

        if(Cross(p[4], p[1], p[3], p[5]) && fabs(Multi(p[4], p[1], p[3])) > eps && fabs(Multi(p[4], p[1], p[5])) > eps){
            map[1][4] = map[4][1] = INF ;
        }
        if(Cross(p[2], p[4], p[3], p[5]) && fabs(Multi(p[2], p[4], p[3])) > eps && fabs(Multi(p[2], p[4], p[5])) > eps)
            map[2][4] = map[4][2] = INF ;

        if(Cross(p[5], p[1], p[3], p[4]) && fabs(Multi(p[1], p[5], p[3])) > eps)
            map[5][1] = map[1][5] = INF ;
        if(Cross(p[2], p[5], p[3], p[4]) && fabs(Multi(p[2], p[5], p[3])) > eps)
            map[2][5] = map[5][2] = INF ;

        Disktra() ;
        printf("%.6lf\n",q[2]) ;
        //fprintf(fout, "%.6lf\n", q[2]) ;
    }
    return 0;
}

 

posted @ 2012-09-13 15:02  3111006139  阅读(207)  评论(0编辑  收藏  举报