POJ 1556 (toj 1952)The Doors

本文介绍了一个使用最短路径算法解决复杂几何问题的例子,通过构建图形并利用算法找出两点间不与任何障碍物相交的最短路径。文章详细解释了如何将几何问题转化为图论问题,并运用最短路径算法求解。

The Doors


Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 126   Accepted Runs: 69


 

The Problem

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.

 

 

Input

The input data for the illustrated chamber would appear as follows.

2
4 2 7 8 9
7 3 4.5 6 7

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input will contain at least one such set of data. The end of the data comes when the number of walls is -1.

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output

10.00
10.06

 

 

Source: Mid-Central USA 1996

 

 

由于交题的时候poj挂了,在hdu上没找到,突然想到自己学校的oj。在toj上一找,发现有这道题,就交了一发,1A,很开心。

发现toj还是挺厉害的,题也是很全的~

思路:乍一看是一道几何题,再一想突然发现可以用最短路做

每个点之间连线,如果不和任何墙相交就建边

最短路跑一下就行了

 

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <iomanip>

#define INF 100000000.0

using namespace std;

struct point
{
    double x,y;
    point(){};
    point (double _x,double _y)
    {
        x=_x,y=_y;
    }
};
typedef point vec;

double operator /(vec a, vec b)
{
    return (a.x*b.y-a.y*b.x);
}
vec operator- (point a,point b)
{
    vec c;
    c.x=a.x-b.x;
    c.y=a.y-b.y;
    return c;
}
struct line
{
    point a,b;
    line(){};
    line (point _a,point _b)
    {
        a=_a;
        b=_b;
    }
};
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

struct edge
{
    int v,next;
    double w;
};
int cross(line m,line n)
{
    if(((n.a-m.a)/(m.b-m.a))*((n.b-m.a)/(m.b-m.a))<=0
       &&((m.a-n.a)/(n.b-n.a))*((m.b-n.a)/(n.b-n.a))<=0)
       return 1;
       else return 0;
}
edge e[5005];
int head[105],cnt=0,vis[5005],n,m;
double dist[105];
queue<int>q;
void addedge(int u,int v,double w)
{
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void spfa(int s)
{
    for(int i=0;i<=n;i++)
        dist[i]=INF,vis[i]=0;
    while(!q.empty()) q.pop();
    q.push(s);
    vis[s]=1;
    dist[s]=0.0;
    while(!q.empty())
    {
        int tmp;
        tmp=q.front();
        q.pop();
        for(int i=head[tmp];i!=-1;i=e[i].next)
        {
            if(dist[e[i].v]>dist[tmp]+e[i].w)
            {
                dist[e[i].v]=dist[tmp]+e[i].w;
                if(!vis[e[i].v])
                {
                    vis[e[i].v]=1;
                    q.push(e[i].v);
                }
            }

        }
        vis[tmp]=0;
    }
}
point p[105];
line le[5005];
bool check(int i,int j)
{
    line temp(p[i],p[j]);
    int left,right;
    if(i!=0 && j!=n-1)
    {
        left=(((i-1)/4)+1)*3;
        right=((j-1)/4)*3;
        for(int c1=left;c1<right;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else if(i==0 && j!=n-1)
    {
        right=((j-1)/4)*3;
        for(int c1=0;c1<right;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else if(i!=0 && j==n-1)
    {
        left=(((i-1)/4)+1)*3;
        for(int c1=left;c1<m-1;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else
    {
        for(int c1=0;c1<m-1;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    int t;
    while(cin>>t)
    {
        if(t==-1)
            break;
        init();
        int totl=0,totp=1;
        n=4*t+2;
        m=3*t;
        p[0].x=0,p[0].y=5.0;
        p[n-1].x=10.0,p[n-1].y=5.0;
        for(int i=0;i<t;i++)
        {
            double x,y1,y2,y3,y4;
            point a,b;
            line tmp;
            cin>>x>>y1>>y2>>y3>>y4;
            p[totp].x=x,p[totp++].y=y1;
            p[totp].x=x,p[totp++].y=y2;
            p[totp].x=x,p[totp++].y=y3;
            p[totp].x=x,p[totp++].y=y4;
            a.x=x,a.y=0.0;
            b.x=x,b.y=y1;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
            a.x=x,a.y=y2;
            b.x=x,b.y=y3;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
            a.x=x,a.y=y4;
            b.x=x,b.y=10.0;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(check(i,j))
                    addedge(i,j,dis(p[i],p[j]));
            }
        }
        spfa(0);
        double ans=dist[n-1];
        cout<<fixed<<setprecision(2)<<ans<<endl;
    }
    return 0;
}

这是16年的还是17年写的 csdn竟然没给我发出去 。。。一口老血

 

 

 

 

内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值