-
Remmarguts' Date
- POJ - 2449
- 题意:就是求第k短路但是有起点终点相等的情况
-
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #define LL long long #define inf 2147483640 using namespace std; const int maxn=100010; struct edge { int to,next,w; } e[maxn<<1]; int head[maxn],cnts[maxn],dis[maxn],vis[maxn]; int n,m,S,T,K,cnt,U[maxn],V[maxn],W[maxn]; struct data { int num,w; friend bool operator < (const data a,const data b) { return a.w>b.w; } }; struct node { int num,g,h; friend bool operator < (const node a,const node b) { return a.g+a.h>b.g+b.h;//返回函数f小的 } }; void link(int u,int v,int w) { e[++cnt]=(edge) { v,head[u],w }; head[u]=cnt; } void Dijkstra() { for (int i=1; i<=n; i++) dis[i]=inf; dis[T]=0; priority_queue<data> q; data x=(data) { T,0 }; q.push(x); while (!q.empty()) { x=q.top(); q.pop(); if (vis[x.num]) continue; vis[x.num]=1; for (int i=head[x.num]; i; i=e[i].next) if (dis[e[i].to]>x.w+e[i].w) { dis[e[i].to]=e[i].w+x.w; q.push((data) { e[i].to,dis[e[i].to] }); } } } int Astar() { node x=(node) { S,0,dis[S] }; priority_queue<node> q; q.push(x); while (!q.empty()) { x=q.top(); q.pop(); cnts[x.num]++; if (cnts[x.num]>K) continue; if (cnts[T]==K) return x.g; for (int i=head[x.num]; i; i=e[i].next) q.push((node) { e[i].to,x.g+e[i].w,dis[e[i].to] }); } return -1; } int main() { scanf("%d%d",&n,&m); for (int i=1; i<=m; i++) { scanf("%d%d%d",&U[i],&V[i],&W[i]); link(V[i],U[i],W[i]); } scanf("%d%d%d",&S,&T,&K); if (S==T) K++; Dijkstra(); memset(head,0,sizeof(head)); cnt=0; for (int i=1; i<=m; i++) link(U[i],V[i],W[i]); int ans=Astar(); printf("%d",ans); return 0; }
Remmarguts' Date-A-STAR
最新推荐文章于 2023-12-12 08:15:00 发布