HDU 6181 Two Paths(A*求K短路)

Problem Description

You are given a undirected graph with n nodes (numbered from 1 to n) and m edges. Alice and Bob are now trying to play a game.
Both of them will take different route from 1 to n (not necessary simple).
Alice always moves first and she is so clever that take one of the shortest path from 1 to n.
Now is the Bob’s turn. Help Bob to take possible shortest route from 1 to n.
There’s neither multiple edges nor self-loops.
Two paths S and T are considered different if and only if there is an integer i, so that the i-th edge of S is not the same as the i-th edge of T or one of them doesn’t exist.

Input

The first line of input contains an integer T(1 <= T <= 15), the number of test cases.
The first line of each test case contains 2 integers n, m (2 <= n, m <= 100000), number of nodes and number of edges. Each of the next m lines contains 3 integers a, b, w (1 <= a, b <= n, 1 <= w <= 1000000000), this means that there’s an edge between node a and node b and its length is w.
It is guaranteed that there is at least one path from 1 to n.
Sum of n over all test cases is less than 250000 and sum of m over all test cases is less than 350000.

Output

For each test case print length of valid shortest path in one line.


题解:
就是一个A*裸题,估价函数f(i) = g(i) + h(i),我们让g(i)表示从源点S到i所经过的路径,h(i)为终点到i的最短路径,那么估值就等于源点到当前点的距离+当前点到终点的距离,然后按估值大小开一个优先队列搜索即可,INF没开long long疯狂MLE…


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define LL long long
const int MAXN = 1e5+50;
const int MOD = 1e9+7;
const LL INF = 1e18;
int n,m,k;LL dis[MAXN];
vector<pair<int,LL> > g[MAXN];
struct A_star{
    int v; LL val;
    bool operator < (A_star x) const{ return x.val+dis[x.v] < val+dis[v]; }
};
inline void dijkstra(){
    priority_queue<pair<LL,int>,vector<pair<LL,int> >,greater<pair<LL,int> > > que;
    que.push(make_pair(0LL,n)); dis[n]=0;
    while(!que.empty()){
        int u=que.top().second; que.pop();
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i].first;LL w=g[u][i].second;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                que.push(make_pair(dis[v],v));
            }
        }
    }
}
inline LL bfs(){
    int kth=0;
    priority_queue<A_star> que;
    que.push(A_star{1,0LL});
    while(!que.empty()){
        A_star u=que.top(); que.pop();
        if(u.v==n) kth++;
        if(kth==2){ return u.val; }
        for(int i=0;i<g[u.v].size();i++){
            que.push(A_star{g[u.v][i].first,u.val+g[u.v][i].second});
        }
    }
    return -1;
}
signed main(){
#ifndef ONLINE_JUDGE
    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int T; scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) g[i].clear(),dis[i]=INF;
        int u,v; LL w;
        for(int i=1;i<=m;i++){
            scanf("%d%d%lld",&u,&v,&w);
            g[u].push_back(make_pair(v,w));
            g[v].push_back(make_pair(u,w));
        }
        dijkstra();
        printf("%lld\n",bfs());
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值