hdu-6181 Two Paths次短路

题意:求次短路。

思路:求次短路

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define siz 200005
#define LL long long
namespace fastIO {
    #define BUF_SIZE 100000
    //fread -> read
    bool IOerror = 0;
    inline char nc() {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if(p1 == pend) {
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1) {
                IOerror = 1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }
    inline void read(int &x) {
        char ch;
        while(blank(ch = nc()));
        if(IOerror)
            return;
        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
    }
    #undef BUF_SIZE
};
using namespace fastIO;
using namespace std;
const LL INF = 1e18;

struct qnode{
    int v;
    LL c;
    qnode(int _v = 0,LL _c = 0) : v(_v),c(_c){}
    bool operator < (const qnode &r) const{
        return c>r.c;
    }
};
struct Edge{
    int v;
    LL cost;
    Edge(int _v = 0,LL _cost = 0) : v(_v),cost(_cost){}
};
int n,m;
vector<Edge>E[siz];
bool vis[siz];
LL dist1[siz];
LL dist2[siz];
void addedge(int u,int v,LL w){
    E[u].push_back(Edge(v,w));
}
void dijstra(int n,int start){
    //memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++){
        dist1[i] = INF;
        dist2[i] = INF;
    }
    priority_queue<qnode> que;
    while(!que.empty()) que.pop();
    dist1[start] = 0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty()){
        tmp = que.top();
        que.pop();
        int u = tmp.v;
        LL d = tmp.c;
        if(dist2[u] < d) continue;
        for(int i = 0;i<E[u].size();i++){
            int v = E[u][i].v;
            LL cost = E[u][i].cost;
            LL d2 = d + cost;
            if(dist1[v] > d2){
                swap(d2,dist1[v]);
                que.push(qnode(v,dist1[v]));
            }
            if(dist2[v] > d2&&dist1[v] <d2){
                dist2[v] = d2;
                que.push(qnode(v,dist2[v]));
            }
        }
    }
    printf("%lld\n",dist2[n]);
}
void solve(){
    dijstra(n,1);
}
int main()
{
    int T;
    //scanf("%d",&T);
    read(T);
    while(T--){
        //scanf("%d%d",&n,&m);
        read(n);
        read(m);
        for(int i=0;i<=n;i++){
            E[i].clear();
        }
        for(int i = 1;i<=m;i++){
            int u,v;
            int w;
            //scanf("%d%d%lld",&u,&v,&w);
            read(u);
            read(v);
            read(w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        solve();
    }
    return 0;
}

保存一波大佬的A*求k短路模板。

A*算法模板在次,点击传送

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值