【模板】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; }
相关文章推荐
- ACM_最短路模板(SPFA,Dijkstra,Floyd)
- POJ 3255 Roadblocks (次短路模板)
- 图论最短路dijkstra----poj1797模板题
- 最短路模板
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
- Hdu 2544 最短路 模板题
- 最短路--dijkstra+优先队列优化模板
- 【模板】最短路&&最小生成树
- 个人模板 最短路djkstra
- HDU2544 - 最短路 - dijkstra/spfa的模板最短路
- Dijkstra算法模板---单源最短路---邻接矩阵模板+题目
- codeforces602C The Two Routes (最短路模板题)
- POJ 1874 畅通工程续(最短路模板题)
- 最短路算法模板合集(Dijkstar,Dijkstar(优先队列优化), 多源最短路Floyd)
- 最短路~dijkstra堆优化模板
- 做了一道跑大数据的最短路挂了,基于vector的二维模拟邻接表实现Dijkstra算法(*【模板】)
- HDOJ2544 最短路(Dijkstra,floyd,模板)
- 最短路模板(dijkstra+邻接表)
- HDU 2544 最短路(我的dijkstra算法模板、SPAFA算法模板)
- 最短路 floyd dijkstra 模板