标签:优先 k短路 pop spfa this size 估计 code for
int cnt; int Astar(){ if(st == en) k++; if(dis[st] == inf) return -1; priority_queue<ptr> q; ptr cur{st, dis[st], 0}; q.push(cur); while(!q.empty()){ ptr t = q.top(); q.pop(); int nt = t.now; if(nt == en) cnt++; if(cnt == k) return t.g; for(int i = 0; i < edges[nt].size(); i++){ int path = t.g + edges[nt][i].w; int now = edges[nt][i].to; q.push(ptr{now, path + dis[now], path}); } } return -1; }
st、en为起点、终点, dis数组为预处理的最短路
ptr结构体的定义:
struct ptr{ int now, F, g; // 当前点编号, 估价函数F, 当前路径g bool operator <(const ptr& a) const { if(this->F == a.F) return this->g > a.g; return this->F > a.F; } };
标签:优先 k短路 pop spfa this size 估计 code for
原文地址:https://www.cnblogs.com/leafsblogowo/p/12749535.html