#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=1e6+5;
int n,m,s,t,k,r,cnt,p1,p2,head1[maxn],head2[maxn],vis[maxn];
ll d[maxn];
struct node
{
int to,w,next;
}e1[maxn],e2[maxn];
struct qnode
{
int v;
ll w;
qnode(int v,ll w):v(w),w(w){}
friend bool operator < (qnode x,qnode y)
{
return x.w+d[x.v]>y.w+d[y.v];
}
};
void add1(int u,int v,int w)
{
e1[++p1].to=v;
e1[p1].w=w;
e1[p1].next=head1[u];
head1[u]=p1;
}
void add2(int u,int v,int w)
{
e2[++p2].to=v;
e2[p2].w=w;
e2[p2].next=head2[u];
head2[u]=p2;
}
void spfa()//反向图,t点到其他点的距离
{
queue<int>q;
q.push(t);
for(int i=1;i<=n;i++) d[i]=inf;
memset(vis,0,sizeof(vis));
d[t]=0;
vis[t]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head1[u];i;i=e1[i].next)
{
int v=e1[i].to;
int w=e1[i].w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
}
ll astar()//A*算法
{
if(d[s]==inf) return -1;
priority_queue<qnode>pe;
pe.push(qnode(s,0));
cnt=0;
while(!pe.empty())
{
qnode u=pe.top();
pe.pop();
if(u.v==t)
{
cnt++;
if(cnt==k) return u.w;
}
for(int i=head2[u.v];i;i=e2[i].next)
{
int v=e2[i].to;
pe.push(qnode(v,u.w+e2[i].w));
}
}
return -1;
}
int main()
{
while(~scanf("%d%d"),&n,&m);
{
memset(head1,0,sizeof(head1));
memset(head2,0,sizeof(head2));
p1=p2=0;
scanf("%d%d%d%d",&s,&t,&k,&r);
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add1(v,u,w);
add2(u,v,w);
}
spfa();
ll ans=astar();
if(ans==-1||ans>r) printf("Whitesnake!\n");
else printf("yareyaredawa\n");
}
system("pause");
return 0;
}
求K短路(A*+dijkstra)模板
最新推荐文章于 2025-05-17 02:08:11 发布