zoukankan      html  css  js  c++  java
  • hdu 6670 Mindis 最短路

    hdu 6670 Mindis 百度之星初赛一

    题意:

    平面上有 n 个矩形,矩形的边平行于坐标轴,现在度度熊需要操控一名角色从 A 点走到 B 点。
    该角色可以上下左右移动,在恰被 k 个矩形覆盖的区域,该角色的速率为 k+1 个距离/秒(矩形覆盖区域包括边界)。

    请求出 A 移动到 B 最快需要多少秒。

    第一行一个整数 T (1≤T≤5) 表示数据组数。
    对于每组数据,第一行输入一个整数 n (1≤n≤200)。
    接下来 n 行每行 4 个整数 x1,y1,x2,y2 (0≤x1<x2≤1000000000,0≤y1<y2≤1000000000),分别表示矩形的左下角和右上角的坐标。
    最后一行四个整数 xa,ya,xb,yb ((0≤xa,xb,ya,yb≤1000000000) 代表 A 和 B 的坐标。

    思路:

    可以看到矩形的数量很小,考虑到会走的“最短路”一定是矩形四个边延申成的网格上走(当然还有起点和终点的延申),那么最多也就是400*400个交点,离散化一下,点和点之间的时间就是距离/速度,速度暴力一下是400*400*4*200也是可以接受的。之后跑个最短路就可以了。

    代码:

    #include <bits/stdc++.h>
    #define LL long long
    #define pii pair<int,int>
    #define PB push_back
    #define X first
    #define Y second
    using namespace std;
    pii A,B;
    int t,n,x1,x2,y1,y2;
    struct rec{
        pii A,B;
    }arr[205];
    const int MAXN=300010;
    struct qnode{
        int v;
        double c;
        qnode(int _v=0,double _c=0):v(_v),c(_c) {}
        bool operator <(const qnode &r)const{
            return c>r.c;
        }
    };
    struct Edge{
        int v;
        double cost;
        Edge(int _v=0,double _cost=0):v(_v),cost(_cost) {}
    };
    vector<Edge>E[MAXN];
    bool vis[MAXN];
    double dist[MAXN];
    void Dijkstra(int n,int start)//点的编号从1开始
    {
        for(int i=1; i<=n; i++)dist[i]=1e9,vis[i]=false;
        priority_queue<qnode>que;
        while(!que.empty())que.pop();
        dist[start]=0;
        que.push(qnode(start,0));
        qnode tmp;
        while(!que.empty()){
            tmp=que.top();
            que.pop();
            int u=tmp.v;
            if(vis[u])continue;
            vis[u]=true;
            for(int i=0; i<E[u].size(); i++){
                int v=E[tmp.v][i].v;
                double cost=E[u][i].cost;
                if(!vis[v]&&dist[v]>dist[u]+cost){
                    dist[v]=dist[u]+cost;
                    que.push(qnode(v,dist[v]));
                }
            }
        }
    }
    void addedge(int u,int v,double w)
    {
        E[u].push_back(Edge(v,w));
        E[v].push_back(Edge(u,w));
    }
    int ok(int a,int b,int c,int d,int n){
        if(a<arr[n].A.X||c>arr[n].B.X||b<arr[n].A.Y||d>arr[n].B.Y) return 0;
        return 1;
    }
    int getv(int x,int y,int x0,int y0){
        int cnt=1;
        for(int i=0;i<n;i++){
            cnt+=ok(x,y,x0,y0,i);
        }
        return cnt;
    }
    int main(){
        cin>>t;
        while(t--){
            vector<int> X;
            vector<int> Y;
            cin>>n;
            for(int i=0;i<n;i++){
                cin>>x1>>y1>>x2>>y2;
                arr[i].A={x1,y1};
                arr[i].B={x2,y2};
                X.PB(x1);X.PB(x2);
                Y.PB(y1);Y.PB(y2);
            }
            cin>>x1>>y1>>x2>>y2;
            X.PB(x1);X.PB(x2);
            Y.PB(y1);Y.PB(y2);
            A={x1,y1};B={x2,y2};
            sort(X.begin(),X.end());
            X.erase(unique(X.begin(),X.end()),X.end());
    
            sort(Y.begin(),Y.end());
            Y.erase(unique(Y.begin(),Y.end()),Y.end());
    
            int num=0;
            int start,end;
            for(int i=0;i<X.size();i++){
                for(int j=0;j<Y.size();j++){
                    int x=X[i],y=Y[j];
                    num++;
                    if(A==make_pair(x,y))start=num;
                    if(B==make_pair(x,y))end=num;
                    if(j+1<Y.size())addedge(num,num+1,(double)(Y[j+1]-y)/getv(x,y,x,Y[j+1]));
                    if(i+1<X.size())addedge(num,num+Y.size(),(double)(X[i+1]-x)/getv(x,y,X[i+1],y));
                }
            }
            Dijkstra(num,start);
            cout<<fixed<<setprecision(5)<<dist[end]<<endl;
            for(int i=0;i<=num;i++)E[i].clear();
        }
        return 0;
    }
    /***
    1
    1
    5 5 6 6
    7 7 8 8
    ***/
    
    
  • 相关阅读:
    The Mac Application Environment 不及格的程序员
    Xcode Plugin: Change Code In Running App Without Restart 不及格的程序员
    The property delegate of CALayer cause Crash. 不及格的程序员
    nil localizedTitle in SKProduct 不及格的程序员
    InApp Purchase 不及格的程序员
    Safari Web Content Guide 不及格的程序员
    在Mac OS X Lion 安装 XCode 3.2 不及格的程序员
    illustrate ARC with graphs 不及格的程序员
    Viewing iPhoneOptimized PNGs 不及格的程序员
    What is the dSYM? 不及格的程序员
  • 原文地址:https://www.cnblogs.com/zhangxianlong/p/11419596.html
Copyright © 2011-2022 走看看