【模板】K短路

来源:互联网 发布:三天打鱼两天晒网c语言 编辑:程序博客网 时间:2024/05/29 17:17

A* + dij

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXN = 200005;int first[MAXN],next[MAXN],tot = 0;int n,m,s,t,K,f,t,v;int h[MAXN],dis[MAXN];bool use[MAXN];struct edge{    int f,t,v;}l[MAXN];struct zt{    int u,v;    bool operator < (const int &b) const    {        return v + h[u] > b.v + h[b.u];    }};void build(int f,int t,int v){    l[++ tot] = (edge){f,t,v};    next[tot] = first[f];    first[f] = tot;    return;}priority_queue < zt > q;void dij(int s){    memset(dis,0x3f,sizeof(dis));    dis[s] = 0;q.push((zt){s,0});    while(!q.empty())    {        zt x = q.top(); p.pop();        int u = x.u;        for(int i = first[u]; i != -1; i = next[i])        {            int w = l[i].t;            if(dis[w] > dis[u] + l[i].v)            {                dis[w] = dis[u] + l[i].v;                q.push((zt){w,dis[w]});            }        }    }    return;}int dij_k(int s,int e,int k){    if(s == e) k ++;    q.push((zt){s,0});    while(!q.empty())    {        zt x = q.top();        int u = x.u;        q.pop();        if(u == e)        {            k --;            if(k == 0)                  return x.v;        }        for(int i = first[u]; i != -1; i = next[i])        {            int w = l[i].t;            int c = x.v + l[i].v;            q.push((zt){w,c});        }    }    return -1;}
0 0
原创粉丝点击