【图论】【A*搜索】[POJ2449]Remmarguts' Date
2015-08-26 20:17
337 查看
题目
本题实质是求第k短的路径;
SPFA+A*
进行启发式搜索直至访问终点k次,
估值函数:当前已经走的距离+当前点到终点的最短路
注意:当s==t时,要特判
本题实质是求第k短的路径;
SPFA+A*
进行启发式搜索直至访问终点k次,
估值函数:当前已经走的距离+当前点到终点的最短路
注意:当s==t时,要特判
[code]#include<cstdio> #include<queue> #include<cstring> #include<cstdlib> using namespace std; #define MAXN 1000 #define MAXM 100000 queue<int>q; struct node2{ int pos,d,f; bool operator < (const node2 &x) const{ return f>x.f; } }; priority_queue<node2>pq; int n,m,dist[MAXN+10],s,t,k; bool vis[MAXN+10]; struct node{ int u,v,wt; node *next; }edge[MAXM*2+10],*adj[MAXN+10],*adj2[MAXN+10],*ecnt=&edge[0]; void Read(int &x){ char c; while((c=getchar())&&c!=EOF) if(c>='0'&&c<='9'){ x=c-'0'; while((c=getchar())&&c>='0'&&c<='9') x=x*10+c-'0'; ungetc(c,stdin); return; } } void addedge(int u,int v,int wt){ node *p=++ecnt; p->v=v; p->wt=wt; p->next=adj[u]; adj[u]=p; } void addedge2(int u,int v,int wt){ node *p=++ecnt; p->v=v; p->wt=wt; p->next=adj2[u]; adj2[u]=p; } void read(){ Read(n),Read(m); int i,u,v,wt; for(i=1;i<=m;i++){ Read(u),Read(v),Read(wt); addedge(u,v,wt); addedge2(v,u,wt); } Read(s),Read(t),Read(k); if(s==t) k++; } void spfa(int s){ q.push(s); dist[s]=0; vis[s]=1; int u,v,wt; while(!q.empty()){ u=q.front(); q.pop(); vis[u]=0; for(node *p=adj2[u];p;p=p->next){ v=p->v,wt=p->wt; if(dist[v]>dist[u]+wt){ dist[v]=dist[u]+wt; if(!vis[v]){ q.push(v); vis[v]=1; } } } } } void Astar(){ node2 u,v; int cnt=0; u.pos=s,u.d=0,u.f=dist[s]; pq.push(u); while(!pq.empty()){ u=pq.top(); if(u.pos==t) cnt++; if(cnt==k){ printf("%d",u.d); exit(0); } pq.pop(); for(node *p=adj[u.pos];p;p=p->next){ v.pos=p->v; v.d=p->wt+u.d; v.f=v.d+dist[p->v]; pq.push(v); } } } int main() { memset(dist,0x7f,sizeof dist); read(); spfa(t); Astar(); printf("-1"); }
相关文章推荐
- 电子商务的本质是低成本高效率
- C++内联函数总结
- codeforces 121C C. Lucky Permutation(数论+构造)
- LeetCode题解:Happy Number
- 浅谈 OneAPM 在 express 项目中的实践
- UI空间大全
- HTML 5 <input> placeholder 属性
- HTML 5 <input> placeholder 属性
- HTML 5 <input> placeholder 属性
- 浅谈 OneAPM 在 express 项目中的实践
- angularjs+requirejs整合
- 6+重建二叉树
- 霍夫变换
- 【图论】[二分图匹配] 模板
- 【图论】[ZOJ1054]Place the Robots
- C语言(6)--国际象棋棋盘、螺旋矩阵、指定年份日历、打印ASCII码表
- Ajax提高篇(7)Ajax实现简单的下拉框联动显示数据
- C++中的指针和指针的引用
- 【图论】[ZOJ1002]Fire Net
- 如何理解file‘s owner