• POJ 3255 Roadblocks (第K短路 & A*)


    Roadblocks
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 5256   Accepted: 2013

    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

    Hint

    Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

    Source

     
     
    题意:有N个点 R个路径(双向的) 起点是1,终点是N 求1到N的次短路
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const int VM=5010;
    const int EM=100010;
    
    int n,m,cnt,src,des;
    int head[VM],vis[VM],dis[VM];
    
    struct Edge{
        int to,nxt;
        int cap;
    }edge[EM<<1];
    
    struct data{
        int to;
        int g,h;
        bool operator < (const data &a) const{
            return a.g+a.h<g+h;
        }
    };
    
    void addedge(int cu,int cv,int cw){
        edge[cnt].to=cv;
        edge[cnt].cap=cw;
        edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
    }
    
    void Dijkstra(){
        int i;
        for(i=1;i<=n;i++){
            dis[i]=INF;
            vis[i]=0;
        }
        dis[des]=0;
        int j,k,tmp;
        for(i=1;i<=n;i++){
            tmp=INF;
            for(j=1;j<=n;j++)
                if(!vis[j] && tmp>dis[j]){
                    tmp=dis[j];
                    k=j;
                }
            if(tmp==INF)
                break;
            vis[k]=1;
            for(int u=head[k];u!=-1;u=edge[u].nxt){
                int v=edge[u].to;
                if(!vis[v] && dis[v]>dis[k]+edge[u].cap)
                    dis[v]=dis[k]+edge[u].cap;
            }
        }
    }
    
    int Astar(){
        priority_queue<data> q;
        while(!q.empty())
            q.pop();
        int count[VM];
        memset(count,0,sizeof(count));
        data cur,next;
        cur.to=src;
        cur.g=0;
        cur.h=dis[src];
        q.push(cur);
        while(!q.empty()){
            cur=q.top();
            q.pop();
            count[cur.to]++;
            if(count[cur.to]>2)
                continue;
            if(count[des]==2)
                return cur.g;
            for(int u=head[cur.to];u!=-1;u=edge[u].nxt){
                int v=edge[u].to;
                next.to=v;
                next.g=cur.g+edge[u].cap;
                next.h=dis[v];
                q.push(next);
            }
        }
        return 0;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&m)){
            cnt=0;
            memset(head,-1,sizeof(head));
            int u,v,w;
            while(m--){
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            src=1;
            des=n;
            Dijkstra();
            int ans=Astar();
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    Making your first driver
    注册表与盘符(转victor888文章 )
    电脑Win7如何取得文件管理所有权(提供各种GHOST版本的Windows)
    可拖动的DIV
    IE Javascript 进阶调试
    优化性能
    命令模式
    MVC 4 结合jquery.uploadify 上传实例
    IIS处理并发请求时出现的问题及解决
    Spring3.2 + Hibernate4.2
  • 原文地址:https://www.cnblogs.com/jackge/p/3053830.html
Copyright © 2020-2023  润新知