=.=好菜

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <queue> using namespace std;
const int N = 1e3+;
const int M = +;
typedef long long ll;
const ll INF = 1e15; int n,m,head[N],rehead[N],tot;
struct node {
int v,w,next;
}E[M],reE[M]; void init() {
tot=;
memset(head,,sizeof(head));
memset(rehead,,sizeof(rehead));
}
void addEdge(int u,int v,int w) {
++tot;
E[tot].next = head[u];
head[u]=tot;
E[tot].v = v;
E[tot].w = w; reE[tot].next = rehead[v];
rehead[v]=tot;
reE[tot].v = u;
reE[tot].w = w;
} bool inq[N]; ll d[N];
queue<int>que;
void spfa(int src) {
while(!que.empty())
que.pop();
for(int i=;i<N;i++) d[i]=INF;
memset(inq,,sizeof(inq));
d[src]=;
inq[src] = ;
que.push(src);
while(!que.empty()) {
int now = que.front();
que.pop();
//if(vis[now]) continue;
//vis[now]=1;
inq[now] = ;
for(int i=rehead[now]; i; i=reE[i].next) {
int v = reE[i].v;
int w = reE[i].w;
if(d[v] > d[now] + w) {
d[v] = d[now] + w;
if(!inq[v])
que.push(v);
}
}
}
} struct A {
int v;
ll f,g;
///v是current点 f(v)=g(v)+h(v) g(v):st到v的估值, h(v):v到ed的估值
bool operator<(const A other) const {
if(other.f == f) return other.g < g;
return other.f < f;
}
}; int Astar(int st,int ed,int k) {
priority_queue<A> Q;
if(st==ed) k++;
if(d[st]==INF) return -;
int cnt = ;
A t,tt;
t.v=st,t.g=,t.f=t.g+d[st];
Q.push(t);
while (!Q.empty()) {
tt = Q.top(); Q.pop();
int u=tt.v;
if(u == ed) {
cnt++;
if(cnt==k) return tt.g;
}
for(int i=head[u]; i; i=E[i].next) {
t.v = E[i].v;
t.g = tt.g + E[i].w;
t.f = t.g + d[t.v];
Q.push(t); }
}
return -;
} int main () {
//freopen("in.txt","r",stdin);
while (scanf("%d %d", &n, &m)==) {
init();
for(int i=;i<=m;i++) {
int x,y,z; scanf("%d%d%d",&x,&y,&z);
addEdge(x,y,z);
}
int s,t,k; scanf("%d%d%d",&s, &t, &k);
spfa(t);
//for(int i=1;i<=n;i++) cout << d[i]<<endl;
cout<<Astar(s,t,k)<<endl;
}
return ;
}

最新文章

  1. POJ 3126 Prime Path
  2. poj3281 Dining
  3. springmvc之DispatcherServlet
  4. [IR] Probabilistic Model
  5. mongodb的监控与性能优化
  6. 【转发】centos 7安装完后出现please make your choice from &#39;1&#39; ......
  7. 灵魂有香气的女子IOS版本APP,近期将考虑开放源代码
  8. apache POI 导出excel相关方法
  9. java面试题集2
  10. BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级( 最短路 )
  11. 使用强类型实体Id来避免原始类型困扰(一)
  12. 1、Linux命令随笔
  13. [Swift]LeetCode844. 比较含退格的字符串 | Backspace String Compare
  14. windows搭建golang环境
  15. 大部分教程不会告诉你的 12 个 JS 技巧
  16. DevExpress VCL Controls 2019发展路线图(No.3)
  17. ADO SQL手写分页
  18. Windows平台下不同版本SVN对比
  19. 使用AndroidStudio运行eclipse开发的app项目
  20. Android开发——RecyclerView特性以及基本使用方法(二)

热门文章

  1. Java非静态内部类为什么不能有静态成员
  2. Loadrunner解决启动浏览器后页面显示空白
  3. 兼容IE7以上的无缝滚动,带箭头、停顿
  4. Python中如何获取类属性的列表
  5. [LeetCode] 1. Two Sum_Easy tag: Hash Table
  6. SpringMyBatisDay03
  7. 7.MySQL必知必会之用通配符进行过滤-like
  8. C# NPOI 操作excel
  9. FormatMessage函数
  10. VS2010/MFC编程入门之五十三(Ribbon界面开发:为Ribbon Bar添加控件)