(1556)POJ

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>

#define LOCAL
#define ll long long
#define lll unsigned long long
#define MAX 1000009
#define eps 1e-8
#define INF 0x7fffffff
#define mod 1000000007

using namespace std;
/*

题意:给你两个固定的点,求不穿过墙的最短距离。

想法:bellman-ford.在写邻接矩阵的时候,要判断两个边是否可以形成。

*/
struct point//点
{
    double x,y;
};
struct edge//边起点终点
{
    int u,v;
};
int n;//房间里墙的数目
double wx[20];//每堵墙的x坐标
point p [109];//存储起点,每个门的两个端点和终点
int psize;//点的数目
double py[20][4];//第i堵墙的4个y坐标
double g[109][109];//邻接矩阵
edge e[109*109];//存储构造的每条边
int esize;//边的数目
int i,j;
double dis(point a,point b)//两点距离
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) );
}
double cross(double x1,double y1,double x2,double y2,double x3,double y3)//判断(x3,y3)点在直线的上方或者下方。
{
    return (x2 - x1)*(y3 - y1) - (x3 - x1)*(y2 - y1);
}
bool isok(point a,point b)//在构造有向网判断两个点是否能连成一条边
{
    if(a.x>=b.x)return 0;
    int flag = 1;
    int i = 0;
    while(wx[i]<=a.x&&i<n) i++;
    while(wx[i]<b.x&&i<n)
    {
        if(cross(a.x,a.y,b.x,b.y,wx[i],0)*cross(a.x,a.y,b.x,b.y,wx[i],py[i][0])<0//a,b被第一段阻挡
           ||cross(a.x,a.y,b.x,b.y,wx[i],py[i][1])*cross(a.x,a.y,b.x,b.y,wx[i],py[i][2])<0//a,b被第二段阻挡
           ||cross(a.x,a.y,b.x,b.y,wx[i],py[i][3])*cross(a.x,a.y,b.x,b.y,wx[i],10)<0)//a,b被第三段阻挡
        {
            flag = 0;break;
        }
        i++;
    }
    return flag;
}
double bellmanford(int beg,int _end)//求起始点到终点的最短距离
{
    double d[109];
    for(int i = 0;i<109;i++)
    {
        d[i] = INF;
    }
    d[beg] = 0;
    bool ex = true;
    for(int i = 0;i<psize&&ex;i++)
    {
        ex = false;
        for(int j = 0;j<esize;j++)
        {
            if(d[e[j].u]<INF&&d[e[j].v]>d[e[j].u]+g[e[j].u][e[j].v])
            {
                d[e[j].v] = d[e[j].u]+g[e[j].u][e[j].v];
                ex = true;
            }
        }
    }
    return d[_end];//返回起点到终点的最短距离
}
void solve()
{
    p[0].x = 0;
    p[0].y = 5;
    psize = 1;
    for(int i = 0;i<n;i++)
    {
        scanf("%lf",&wx[i]);
        for(int j = 0;j<4;j++)
        {
            p[psize].x = wx[i];
            scanf("%lf",&p[psize].y);
            py[i][j] = p[psize].y;
            psize++;
        }
    }
    p[psize].x = 10;
    p[psize].y = 5;
    psize++;
    for(int i = 0;i<psize;i++)
    {
        for(int j = i+1;j<psize;j++)
        {
            g[i][j] = INF;
        }
    }
    esize = 0;//边的数目
    for(int i = 0;i<psize;i++)
    {
        for(int j = i+1;j<psize;j++)
        {
            if(isok(p[i],p[j]))
            {
                //cout<<"Case1: "<<p[i].x<<" "<<p[i].y<<endl;
                //cout<<"Case2: "<<p[j].x<<" "<<p[i].y<<endl;
                g[i][j] = dis(p[i],p[j]);
                e[esize].u = i;
                e[esize].v = j;
                esize++;
            }
        }
    }
    printf("%.2lf\n",bellmanford(0,psize - 1));
}
int main()
{
    //#ifdef LOCAL
      //freopen("date.in","r",stdin);
       //freopen("date.out","w",stdout);
    //#endif // LOCAL
    while(scanf("%d",&n)!=EOF)
    {
        if(n==-1)break;
        solve();
    }
    return 0;
}

你可能感兴趣的:((1556)POJ)