[第2短路变形]LightOJ 1099 - Not the Best

1099 - Not the Best
Time Limit: 2 second(s)Memory Limit: 32 MB

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

The countryside consists of R bidirectional roads, each linking two of the N intersections, conveniently numbered from 1 to N. Robin starts at intersection 1, and his 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

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains two integers N (1 ≤ N ≤ 5000) and R (1 ≤ R ≤ 105). Each of the next R lines contains three space-separated integers: u, v and w that describe a road that connects intersections u and v and has length w (1 ≤ w ≤ 5000).

Output

For each case, print the case number and the second best shortest path as described above.

Sample Input

Output for Sample Input

2

3 3

1 2 100

2 3 200

1 3 50

4 4

1 2 100

2 4 200

2 3 250

3 4 100

Case 1: 150

Case 2: 450

 


PROBLEM SETTER: JANE ALAM JAN

 

传送门:http://lightoj.com/volume_showproblem.php?problem=1099

 

题意:对第2短路做了一个新的定义,如果存在多条最短路,那么一条比最短路长,而且比其他所有路径短的路径才是第2短路。

 

思路:和上一题差不多,直接用A*,然后改变退出条件即可,即 cur.f > dist[src]

 

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 5555,MAXM = 111111;
int inQ[MAXN],dist[MAXN],t,n,m,head[MAXN],ecnt;
struct edge{
    int v,w,nxt;
    edge(){nxt=-1;}
    edge(int vv,int ww,int nx):v(vv),w(ww),nxt(nx){}
}next[MAXM<<1];
void init(){
    memset(inQ,0,sizeof(inQ));
    fill(dist,dist+MAXN,0x3fffffff);
    memset(head,-1,sizeof(head));
    ecnt = 0;
}
void add(int u,int v,int w){
    next[ecnt++] = edge(v,w,-1);
    next[ecnt++] = edge(u,w,-1);
    next[ecnt-2].nxt = head[u];
    head[u] = ecnt-2;
    next[ecnt-1].nxt = head[v];
    head[v] = ecnt-1;
}
struct node{
    int f,g,v;
    node(){}
    node(int ff,int gg,int vv):f(ff),g(gg),v(vv){}
    bool operator < (const node &n) const{
        return n.f < f;
    }
};
void spfa(int src){
    queue<int> Q;
    Q.push(src);
    inQ[src] = 1;
    dist[src] = 0;
    while(!Q.empty()){
        int now = Q.front();
        Q.pop();inQ[now]=0;
        for(int i=head[now];i!=-1;i=next[i].nxt){
            if(dist[next[i].v]>dist[now]+next[i].w){
                dist[next[i].v] = dist[now]+next[i].w;
                if(!inQ[next[i].v]){
                    inQ[next[i].v]=1;
                    Q.push(next[i].v);
                }
            }
        }
    }
}
int Astar(int src){
    priority_queue<node> PQ;
    memset(inQ,0,sizeof(inQ));
    PQ.push(node(dist[src],0,src));
    while(!PQ.empty()){
        node cur = PQ.top();
        PQ.pop();inQ[cur.v]++;
        //cout<<cur.f<<endl;
        if(cur.f>dist[src]){
            return cur.f;
        }
        for(int i=head[cur.v];i!=-1;i=next[i].nxt){
            node expand(cur.g+dist[next[i].v]+next[i].w,cur.g+next[i].w,next[i].v);
            PQ.push(expand);
        }
    }
    return -1;
}
int main(){
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        scanf("%d%d",&n,&m);
        init();
        while(m--){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);a--,b--;
            add(a,b,c);
        }
        spfa(n-1);
        printf("Case %d: %d\n",cas,Astar(0));
    }
    return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值