模板题:POJ-2449
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define maxm 2000000
#define maxn 10000
using namespace std;
struct edge{
int v,w;
int next;
}e[maxm], e2[maxm];
int cnt = 0, k;
int head[maxn],head2[maxn];
int dis[maxn], vis[maxn];
struct node{
int u, w, f;
bool operator <(const node a) const{
if(a.f==f)
return a.w < w;
return a.f < f;
}
};
void addedge(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt;
e2[cnt].v = u;
e2[cnt].w = w;
e2[cnt].next = head2[v];
head2[v] = cnt++;
}
void spfa(int t)
{
memset(dis, 0x3f, sizeof dis);
dis[t] = 0;
int inq[maxn];
memset(inq, 0, sizeof inq);
queue<int> q;
q.push(t);
inq[t] = 1;
while(!q.empty())
{
int x = q.front();
q.pop(); inq[x] = 0;
for(int i=head2[x];i!=-1;i=e2[i].next)
{
if(dis[x]+e2[i].w < dis[e2[i].v])
{
dis[e2[i].v] = dis[x]+e2[i].w;
if(!inq[e2[i].v]){
q.push(e2[i].v);
inq[e2[i].v] = 1;
}
}
}
}
}
int a_star(int s, int t)
{
priority_queue<node> q;
if(s==t) k++;
if(dis[s]==0x3f3f3f3f) return -1;
q.push(node{s, 0, 0+dis[s]});
int kcnt = 0;
while(!q.empty())
{
int u = q.top().u, w = q.top().w;
if(u==t)
{
kcnt++;
if(kcnt==k){
return w;
}
}
q.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
q.push(node{e[i].v, w+e[i].w, w+e[i].w+dis[e[i].v]});
}
}
return -1;
}
int main()
{
int m,n, u, v, w, s, t;
while(cin >> n >> m){
memset(head, -1, sizeof head);
memset(head2, -1, sizeof head2);
for(int i=0;i<m;i++)
{
scanf("%d %d %d", &u,&v,&w);
addedge(u, v, w);
}
scanf("%d %d %d",&s, &t, &k);
spfa(t);
cout << a_star(s, t) << endl;
}
return 0;
}