A*模板题
#include<cstdio>
#include<queue>
#include<cstring>
#define MAXN 1005
#define MAXM 100005
using namespace std;
struct edge{int next,to,val;}e[MAXM<<1];
bool vis[MAXN];
int cnt_edge=0, cnt[MAXN], last1[MAXN], last2[MAXN], dis[MAXN];
void add(int a, int b, int c)
{
e[++cnt_edge]=(edge){last1[a],b,c};
last1[a]=cnt_edge;
e[++cnt_edge]=(edge){last2[b],a,c};
last2[b]=cnt_edge;
}
void SPFA(int beg)
{
queue<int> q;
memset(dis,64,sizeof(dis));
dis[beg]=0;
q.push(beg);
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=0;
for(int i = last2[x]; i; i=e[i].next)
{
int y=e[i].to;
if(dis[x]+e[i].val<dis[y])
{
dis[y]=dis[x]+e[i].val;
if(!vis[y])
{
vis[y]=1;
q.push(y);
}
}
}
}
}
struct node
{
int p, g, h;
bool operator < (node a) const
{
return a.g+a.h<g+h;
}
};
int A_star(int beg, int end, int k)
{
priority_queue<node> q;
node cur=(node){beg,0,dis[beg]}, nxt;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
cnt[cur.p]++;
if(cnt[cur.p]>k)continue;
if(cnt[end]==k)return cur.g;
for(int i = last1[cur.p]; i; i=e[i].next)
{
nxt=(node){e[i].to,cur.g+e[i].val,dis[e[i].to]};
q.push(nxt);
}
}
return -1;
}
int main()
{
int n, m, s, t, k;
scanf("%d%d",&n,&m);
for(int i = 1, a, b, c; i <= m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
scanf("%d%d%d",&s,&t,&k);
SPFA(t);
if(s==t)k++;//卧槽巨坑
int ans=A_star(s, t, k);
printf("%d\n",ans);
}