zoukankan      html  css  js  c++  java
  • poj3255 Roadblocks 次短路

    Roadblocks
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10098   Accepted: 3620

    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: A, B, 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

    套的A*模板,注意最短路可能不止一条
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #define Max 10000
    #define inf 1<<28
    using namespace std;
    int S,T,K,n,m;
    int head[Max],rehead[Max];
    int num,renum;
    int dis[Max];
    bool visit[Max];
    int ans[Max];
    int qe[Max*10];
    struct kdq{
        int v,len,next;
    } edge[200005],reedge[200005];
    
    struct a_star {               //A*搜索时的优先级队列;
        int v;
        int len;
        bool operator<(const a_star &a)const{    //f(i)=d[i]+g[i]
            return len+dis[v]>a.len+dis[a.v];
        }
    };
    void insert(int u,int v,int len){//正图和逆图
        edge[num].v=v;
        edge[num].len=len;
        edge[num].next=head[u];
        head[u]=num;
        num++;
        reedge[renum].v=u;
        reedge[renum].len=len;
        reedge[renum].next=rehead[v];
        rehead[v]=renum;
        renum++;
    }
    
    void init(){
        memset(ans,0,sizeof(ans));
        for(int i=0; i<=n; i++)
            head[i]=-1,rehead[i]=-1;
        num=1,renum=1;
    }
    int ans1;
    void spfa(){//从T开始求出T到所有点的 dis[]
        int i,j;
        for(i=1; i<=n; i++)
            dis[i]=inf;
        dis[T]=0;
        visit[T]=1;
        int num=0,cnt=0;
        qe[num++]=T;
        while(num>cnt){
            int temp=qe[cnt++];
            visit[temp]=0;
            for(i=rehead[temp]; i!=-1 ; i=reedge[i].next){
                int tt=reedge[i].v;
                int ttt=reedge[i].len;
                if(dis[tt]>dis[temp]+ttt)
                {
                    dis[tt]=dis[temp]+ttt;
                    if(!visit[tt])
                    {
                        qe[num++]=tt;
                        visit[tt]=1;
                    }
                }
            }
        }
    }
    int A_star(){
        if(S==T)
            K++;
        if(dis[S]==inf)
            return -1;
        a_star n1;
        n1.v=S;
        n1.len=0;
        priority_queue <a_star> q;
        q.push(n1);
        while(!q.empty()){
            a_star temp=q.top();
            q.pop();
            ans[temp.v]++;
            if(ans[T]==K){//当第K次取到T的时候,输出路程
                if(temp.len==ans1)
                    K++;
                else
                return temp.len;
                }
            if(ans[temp.v]>K)
                continue;
            for(int i=head[temp.v]; i!=-1; i=edge[i].next){
                a_star n2;
                n2.v=edge[i].v;
                n2.len=edge[i].len+temp.len;
                q.push(n2);
            }
        }
        return -1;
    }
    int main(){
        int i,j,k,l;
        int a,b,s;
        while(scanf("%d%d",&n,&m)!=EOF){
          init();
        while(m--){
            scanf("%d%d%d",&a,&b,&s);
            insert(a,b,s);
            insert(b,a,s);
        }
        S=1,T=n,K=2;
        spfa();
        ans1=dis[S];
      //  printf("%d
    ",ans1);
        printf("%d
    ",A_star());
        }
        return 0;
    }
  • 相关阅读:
    TOC 1. TODO springboot优雅关机
    SpringBoot实现优雅的关机
    Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现
    正确、安全地停止SpringBoot应用服务
    springboot优雅关机
    简单几步教你实现移动硬盘PE、装win7/vista! 一盘在手,系统无忧!
    设置vista和win7进入Debug模式
    杀毒绝招:用“记事本” 处理顽固程序(命令行修改默认打开方式)
    win7 UAC bypass(微软已经给予了三组组件绕过UAC启动的特权)
    Win7,Vista UAC下应用程序标注为“需要管理员权限”的四种方法(可以修改注册表)
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4857566.html
Copyright © 2011-2022 走看看