zoukankan      html  css  js  c++  java
  • hihocoder 1138 Islands Travel dijkstra+heap 难度:2

    http://hihocoder.com/problemset/problem/1138

    很久不用最短路,几乎连基本性质也忘了,结果这道题就是某些最短路算法空间复杂度是o(n)

    这里总结四种算法

    算法名称           时间复杂度       空间复杂度

    dijkstra+heap  O(elog(e+n))   O(n)

    bellman-ford    O(ne)             O(n)

    spfa                O(ke)             O(n)

    floyd-warshall   O(n^3)          O(n^2)

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    
    using namespace std;
    const int maxn = 1e5 + 5;
    const int maxm = 1e6 + 6;
    
    int first[maxn],n;
    struct edge
    {
        int t,c,nxt;
    } e[maxm];
    
    void addedge(int f,int t,int c,int ind)
    {
        e[ind].nxt = first[f];
        e[ind].t = t;
        e[ind].c = c;
        first[f] = ind;
    }
    
    struct pnt
    {
        int x,y,id;
        pnt()
        {
            x = y = id = 0;
        }
        pnt(int _x,int _y,int _id)
        {
            x = _x;
            y = _y;
            id = _id;
        }
    };
    bool cmpx(pnt p1,pnt p2)
    {
        if(p1.x!= p2.x)return p1.x < p2.x;
        return p1.y < p2.y;
    }
    bool cmpy(pnt p1,pnt p2)
    {
        if(p1.y!= p2.y)return p1.y < p2.y;
        return p1.x < p2.x;
    }
    pnt a[maxn];
    long long dis[maxn];
    bool vis[maxn];
    typedef pair<long long ,int> P;
    priority_queue<P, vector <P>, greater<P> > que;
    long long dijkstra()
    {
        for(int i = 0; i < n; i++)dis[i] = 2e18;
        memset(vis,false,sizeof vis);
        while(!que.empty())que.pop();
    
    
        dis[0] = 0;
        vis[0] = true;
        for(int p = first[0]; p != -1; p = e[p].nxt)
        {
            int t = e[p].t;
            dis[t] = e[p].c;
            que.push(P(dis[t],t));
        }
    
        while(!que.empty())
        {
            int f = que.top().second;
            que.pop();
            if(f == n-1)break;
            if(vis[f])continue;
            vis[f] = true;
    
            for(int p = first[f]; p != -1; p = e[p].nxt)
            {
                int t = e[p].t;
                if(dis[t] > dis[f] + e[p].c){
                    dis[t] = dis[f] + e[p].c;
                    que.push(P(dis[t],t));
                }
            }
        }
    
        return dis[n - 1];
    }
    int main()
    {
        while(scanf("%d",&n)==1)
        {
            memset(first, -1, sizeof first);
            for(int i = 0; i < n; i++)
            {
                scanf("%d%d",&a[i].x,&a[i].y);
                a[i].id = i;
            }
    
            sort(a,a + n,cmpx);
            for(int i = 0; i < n - 1; i++)
            {
                addedge(a[i].id,a[i + 1].id,
                        min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i);
                addedge(a[i + 1].id,a[i].id,
                        min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i + 1);
            }
            sort(a,a + n,cmpy);
            for(int i = 0; i < n - 1; i++)
            {
                addedge(a[i].id,a[i + 1].id,
                        min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i + 2 * n);
                addedge(a[i + 1].id,a[i].id,
                        min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i + 1 + 2 * n);
            }
    
            long long ans = dijkstra();
            printf("%lld
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    巧用SQL生成SQL语句
    update,delete与INNER JOIN 以及删除重复数据
    sql判断各种类型的东西存在与否(参考)
    附加数据库报错823
    hibernate配置文件hibernate.cfg.xml的详细解释
    向数据库插入图片以及从数据库中读取图片并显示到jsp(数据库中存储的图片字段类型为Blob或image)
    Hibernate中hibernateTemplate()方法总结
    spring MVC之构造ModelAndView对象
    手机网站初步布局,如何建立你自己的手机网站?
    (MoMoCMS教程2)创建页面——主菜单
  • 原文地址:https://www.cnblogs.com/xuesu/p/4516001.html
Copyright © 2011-2022 走看看