您的位置:首页 > 其它

【模板】K短路

2016-11-11 08:55 218 查看
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(); q.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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: