poj1556 判断线段相交+最短路

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
#define rd(x) scanf("%d",&x)
#define rdd(x,y) scanf("%d%d",&x,&y)
#define rddd(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define rds(s) scanf("%s",s)
#define rep(i,n) for(int i=0;i<n;i++)
#define LL long long
const int N = 100;
const int M=5e5+10;
const int inf=1e9;
const double eps=1e-5;
const int MOD=1e9+7;
int n,m;
struct Point{
    double x,y;
    Point(){

    }
    Point(double _x,double _y){
        x=_x;y=_y;
    }
};
Point p1,p2,p3,p4;
struct Seg{
    Point s,e;
    Seg(){}
    Seg(Point _s,Point _e){
        s=_s;
        e=_e;
    }
    Seg(double x,double y,double xx,double yy){
        s.x=x;s.y=y;
        e.x=xx;e.y=yy;
    }
};
double cross(Point a,Point b,Point c){
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dist(Point a,Point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int sgn(double x){ return x<-eps?-1:x<eps?0:1;}
void solve( ){

}

int pn,ps;
int tot;
Seg seg[500];
Point point[500];
int head[500];
struct Edge{
    int to,nxt;
    double w;
}edge[500*500];
void addEdge(int u,int v,double w){
    edge[tot].to=v;
    edge[tot].nxt=head[u];
    edge[tot].w=w;
    head[u]=tot++;
}
bool jiao(Point a,Point b){

    for(int i=0;i<ps;i++){
        if(   seg[i].s.x<b.x && seg[i].s.x>a.x  &&
             sgn(cross(a,b,seg[i].s))*sgn(cross(a,b,seg[i].e))<=0
        ) return true;
    }
    return false;
}
bool vis[500];
double dis[500];
queue<int> q;
int main()
{
#ifndef ONLINE_JUDGE
freopen("aaa","r",stdin);
#endif
    int T;
    while(true){
        rd(n);
        if(n==-1) break;
        ps=0;
        pn=0;
        point[pn++]=Point(0,5);
        for(int i=1;i<=n;i++){
            double u,v1,v2,v3,v4;
            scanf("%lf%lf%lf%lf%lf",&u,&v1,&v2,&v3,&v4);
            seg[ps++]=Seg(u,0,u,v1);
            seg[ps++]=Seg(u,v2,u,v3);
            seg[ps++]=Seg(u,v4,u,10);

            point[pn++]=Point(u,v1);
            point[pn++]=Point(u,v2);
            point[pn++]=Point(u,v3);
            point[pn++]=Point(u,v4);

        }
         point[pn++]=Point(10,5);

         tot=0;
         memset(head,-1,sizeof head);

      for(int i=0;i<pn;i++)
            for(int j=i+1;j<pn;j++) if(!jiao(point[i],point[j])){
                addEdge(i,j,dist(point[i],point[j]));
            }

           for(int i=0;i<pn;i++){
            dis[i]=inf;
            vis[i]=0;
           }
            while(!q.empty()) q.pop();
            vis[0]=1;
            dis[0]=0;
            q.push(0);
            while(!q.empty()){
                int u=q.front();q.pop();
                vis[u]=0;
                for(int k=head[u];~k;k=edge[k].nxt){
                    int v=edge[k].to;
                    double w=edge[k].w;
                    if(dis[v]>dis[u]+w){
                                dis[v]=dis[u]+w;
                                if(!vis[v]){
                                    vis[v]=1;
                                    q.push(v);
                                }
                    }
                }
            }
           printf("%.2f\n",dis[pn-1]);
    }


    return 0;
}

你可能感兴趣的:(poj1556 判断线段相交+最短路)