码迷,mamicode.com
首页 > 数据库 > 详细

poj3255 Roadblocks 次短路

时间:2015-10-06 20:43:05      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

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\n",ans1);
    printf("%d\n",A_star());
    }
    return 0;
}

 

poj3255 Roadblocks 次短路

标签:

原文地址:http://www.cnblogs.com/13224ACMer/p/4857566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!