SWUST OJ 1025

地址:http://acm.swust.edu.cn/oj/problem/1025/


Description

TT lives in a dream world.All countries in the world are circular or rectangular.There is a moat on the territorial border of each country.(the edge of the circle or the rectangle).The water in the moat flow in one direction,so people in the country can travel in the moat by ship.

Now there are N countries numbered 1,2,3……N.TT is in the moat of the country numbered 1.He wants to get to the country numbered N.(get to the country's moat)How much distance does TT have to walk(not by ship) at least?

Input

The input consists of multiple test cases.

For each case,the first line contains one integer N(2<=N<=200),the number of countries in this world,then N lines follow,the ith line indicates the information of the country numbered i.Here is the detail:

C X Y R:indicate the country numbered i is a circle,and the center of the circle is(X,Y),the radius is R.(0<=R<=10^6)

R X1 Y1 X2 Y2(X1!=X2,Y1!=Y2):indicate the country numbered i is a rectangle,and the two vertexs of one diagonal are (X1,Y1) and (X2,Y2). (-10^6

We guarantee that any two countries don't overlap.

The input terminates with the integer 0. 

Output

The minimum distance TT has to walk.Please keep two decimal places.

Sample Input

2
C 0 0 1
R 1 5 2 6
3
C 0 0 1
C 2 2 1
R 3 0 4 1
0

Sample Output

4.10
1.24
Hint
由于OJ上传数据的BUG,换行请使用"\r\n",非常抱歉
Source

求点1到点n的最短距离,保留2位小数,这部分很裸的最短路

关键是把边的距离求出来,它的点是圆或者矩形(无重叠),要求出两个图形的最短距离.

圆和圆的距离就是圆心距减去半径和

圆到矩形的距离,可以把矩形看成4条线段,也就是圆心到线段的距离

矩形到矩形的距离,分别看成4条线段....


#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define eps 1e-6
#define PI acos(-1.0)

struct Point{
    double x,y;
    Point(double _x,double _y):x(_x),y(_y){}
};
struct Seg{
    Point a,b;
    Seg(Point _a,Point _b):a(_a),b(_b){}
};
struct Circle{
    Point cen;
    double r;
    int id;
    Circle(Point _cen,double _r,int _id):cen(_cen),r(_r),id(_id){}
};
struct Rec{
    Point a,b;
    int id;
    Rec(Point _a,Point _b,int _id):a(_a),b(_b),id(_id){}
};

//点之间的距离
inline double len(Point a,Point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

//余弦定理算夹角
inline double angle(Point a,Point b,Point c){
    double l1=len(a,b);
    double l2=len(a,c);
    double l3=len(b,c);
    return acos((l1*l1+l2*l2-l3*l3)/(2*l1*l2));
}

//点到线段的距离
inline double dis(Point a,Seg b){
    double ang=angle(b.a,a,b.b);
    if(ang<(PI/2) || fabs(ang-PI/2)cir;
vectorrec;
double map[205][205],ans[205];
bool vis[205];

inline double spfa(){
    queueq;
    q.push(1);
    for(int i=1;i<=n;i++) ans[i]=1000000000.0;
    ans[1]=0.0;
    memset(vis,0,sizeof(vis)),vis[1]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop(),vis[x]=0;
        for(int i=1;i<=n;i++){
            if(i!=x && map[x][i]!=1000000000.0){
                double l=ans[x]+map[x][i];
                if(lc) swap(x,c),swap(y,d);
                rec.push_back(Rec(Point(x,y),Point(c,d),i));
            }
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                map[i][j]=1000000000.0;
            }
            map[i][i]=0.0;
        }
        
        //求出任意两个物体之间的最短距离
        for(int i=0;il){
                    map[now][a]=map[a][now]=l;
                }
            }
            for(int j=0;jl){
                    map[now][a]=map[a][now]=l;
                }
            }
        }
        for(int i=0;il){
                    map[now][a]=map[a][now]=l;
                }
            }
        }
        printf("%.2lf\r\n",spfa());
    }
    return 0;
}







你可能感兴趣的:(ACM)