zoukankan      html  css  js  c++  java
  • POJ3255(次短路)

    Roadblocks
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12480   Accepted: 4416

    Description

    Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

    The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

    The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

    Input

    Line 1: Two space-separated integers: N and R 
    Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

    Output

    Line 1: The length of the second shortest path between node 1 and node N

    Sample Input

    4 4
    1 2 100
    2 4 200
    2 3 250
    3 4 100

    Sample Output

    450
    次短路两种解法:spfa迭代、spfa+A*

    spfa迭代:
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int MAXN=5005;
    const int INF=0x3f3f3f3f;
    struct Edge{
        int to,w,net;
    }es[200005];
    int n,m;
    int head[MAXN],tot;
    void addedge(int u,int v,int w)
    {
        es[tot].to=v;
        es[tot].w=w;
        es[tot].net=head[u];
        head[u]=tot++;
    }
    
    int d[MAXN],vis[MAXN],second[MAXN];
    void spfa(int src)
    {
        for(int i=1;i<=n;i++)
        {
            second[i]=INF;
            d[i]=INF;
            vis[i]=0;
        }
        d[src]=0;
        vis[src]=1;
        queue<int> que;
        que.push(src);
        while(!que.empty())
        {
            int u=que.front();que.pop();    
            vis[u]=0;
            for(int i=head[u];i!=-1;i=es[i].net)
            {
                Edge e=es[i];
                bool tag=false;
                if(d[e.to]>d[u]+e.w)
                {
                    second[e.to]=d[e.to];
                    d[e.to]=d[u]+e.w;
                    tag=true;
                }
                else if(d[e.to]<d[u]+e.w&&d[u]+e.w<second[e.to])
                {
                    second[e.to]=d[u]+e.w;
                    tag=true;
                }
                if(second[e.to]>second[u]+e.w)
                {
                    second[e.to]=second[u]+e.w;
                    tag=true;
                }
                if(tag)
                {
                    if(!vis[e.to])
                    {
                        vis[e.to]=1;
                        que.push(e.to);
                    }
                }
            }
        }
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            memset(head,-1,sizeof(head));
            tot=0;
            for(int i=0;i<m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            spfa(1);    
            printf("%d
    ",second[n]);
        }
        return 0;
    }

     spfa+A*算法

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int MAXN=5005;
    const int INF=0x3f3f3f3f;
    struct Edge{
        int to,w,net;
    }es[200005];
    int n,m;
    int head[MAXN],tot;
    void addedge(int u,int v,int w)
    {
        es[tot].to=v;
        es[tot].w=w;
        es[tot].net=head[u];
        head[u]=tot++;
    }
    
    int d[MAXN],vis[MAXN];
    void spfa(int src)
    {
        for(int i=1;i<=n;i++)
        {
            d[i]=INF;
            vis[i]=0;
        }
        queue<int> que;
        d[src]=0;
        que.push(src);
        while(!que.empty())
        {
            int u=que.front();que.pop();
            vis[u]=0;
            for(int i=head[u];i!=-1;i=es[i].net)
            {
                if(d[es[i].to]>d[u]+es[i].w)
                {
                    d[es[i].to]=d[u]+es[i].w;
                    if(!vis[es[i].to])
                    {
                        vis[es[i].to]=1;
                        que.push(es[i].to);
                    }
                }
            }
        }
    }
    
    struct Node{
        int u,g,f;
        Node(){}
        Node(int u,int g,int f)
        {
            this->u=u;
            this->g=g;
            this->f=f;
        }
        bool operator<(const Node e) const
        {
            return e.f < f;
        }
    };
    void Astar(int src,int ter)
    {
        priority_queue<Node> que;
        que.push(Node(src,0,d[src]));
        
        int k=0;
        while(!que.empty())
        {
            Node now=que.top();que.pop();
            if(now.u==ter)
            {
                k++;
                if(k==2)
                {
                    printf("%d
    ",now.f);
                    return ;
                }
            }
            
            for(int i=head[now.u];i!=-1;i=es[i].net)
            {
                que.push(Node(es[i].to,now.g+es[i].w,now.g+es[i].w+d[es[i].to]));    
            }
        }
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            memset(head,-1,sizeof(head));
            tot=0;
            for(int i=0;i<m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            spfa(n);    
            Astar(1,n);
        }
        return 0;
    }
  • 相关阅读:
    细说linux IPC(四):posix 共享内存
    百世经纶一页书
    【机器学习PAI实践二】人口普查统计
    JOptionPane的经常使用4种对话框
    linux pip install gdal apyt-get install libgdal-dev export cplus_path=/usr/include/gdal export c_path=/usr/include/gdal
    linux too many files
    ab 压测
    centos6.6 ftp 配置 修改默认端口等
    openstack instance change storage dir
    事件冒泡(转)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5816734.html
Copyright © 2011-2022 走看看