poj 2449 Remmarguts' Date 第k短路 (最短路变形)
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 33606 | Accepted: 9116 |
Description
"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission."
"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)"
Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help!
DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate.
Input
The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14 这题就是一个第k短路的模板题,
主要是运用Astar算法,启发式搜索,
就是相当于一个剪枝,优化的非常明显,
struct A
{
int v,g,f;
bool operator < (const A a)const {
if (a.f==f ) return a.g<g;
return a.f<f;
}
};
v表示所在点,g表示到达v点所走的距离,
f表示 走到终点 的距离,
得到f 就需要反跑一遍最短路,d[maxn]就是用来存储这个距离的
所以 f=g+d[s]
这题其实我一开始爆内存了好多发 ,找了一天发现是我重载写的有问题
但是我不知道问题到底在那里
有 大佬指点下吗
struct A {
int f, g, v;
friend bool operator <(A a, A b) {
if (a.f == b.f ) return a.g < b.g;
return a.f < b.f;
}
};
希望有大佬能告诉我 这样写重载为什么会导致爆内存!
表示我特别迷啊 ,卡了一天结果是这里有问题 ,
更加可悲的事,我还不知道原因是什么。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue> using namespace std;
const int maxn =1e3+;
const int maxm = 5e5+;
const int inf = 1e9+;
struct node
{
int v,w,next;
}edge[maxm],redge[maxm];
int vis[maxn],d[maxn],head[maxn],rhead[maxn];
int e,s,t,n,m,k;
struct A
{
int v,g,f;
bool operator < (const A a)const {
if (a.f==f ) return a.g<g;
return a.f<f;
}
};
void init()
{
e=;
memset(head,-,sizeof(head));
memset(rhead,-,sizeof(rhead));
}
void add(int x,int y,int z)
{
edge[e].v=y;
edge[e].w=z;
edge[e].next=head[x];
head[x]=e;
redge[e].v=x;
redge[e].w=z;
redge[e].next=rhead[y];
rhead[y]=e;
e++;
}
void spfa(int s)
{
for (int i= ;i<=n ;i++) d[i]=inf;
memset(vis,,sizeof(vis));
d[s]=;
vis[s]=;
queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for (int i=rhead[u] ;i!=- ;i=redge[i].next) {
int v=redge[i].v;
int w=redge[i].w;
if (d[v]>d[u]+w){
d[v]=d[u]+w;
if (!vis[v]) {
q.push(v);
vis[v]=;
}
}
}
}
}
int Astar(int s,int des)
{
int cnt=;
if (s==des) k++;
if (d[s]==inf) return -;
priority_queue<A>q;
A t,tt;
t.v=s,t.g=,t.f=t.g+d[s];
q.push(t);
while(!q.empty()){
tt=q.top();
q.pop();
if (tt.v==des) {
cnt++;
if (cnt==k) return tt.g;
}
for (int i=head[tt.v] ;i!=- ; i=edge[i].next ){
t.v=edge[i].v;
t.g=edge[i].w+tt.g;
t.f=t.g+d[t.v];
q.push(t);
}
}
return -;
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF ){
init();
for (int i= ;i<=m ;i++) {
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
add(x,y,c);
}
scanf("%d%d%d",&s,&t,&k);
spfa(t);
printf("%d\n",Astar(s,t));
}
return ;
}
poj 2449 Remmarguts' Date 第k短路 (最短路变形)的更多相关文章
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- POJ 2449 Remmarguts' Date (第k短路径)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions:35025 Accepted: 9467 ...
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
随机推荐
- Lock(三)查看是谁把表给锁了
查看是谁把表给锁了 select se1.inst_id as 被阻塞的会话节点, se2.inst_id as 罪魁祸首节点, se1.sid as 被阻塞的会话ID, ob.object_name ...
- 阿里云API网关(2)开放 API 并接入 API 网关
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- ibatis的优缺点及可行性分析
1.优点 简单: 易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现. 实用: 提供了数据映射功能,提供了对底层数据访问的封装(例如ado.net),提供了DAO框架,可以使我 ...
- Dijkstra的双栈算术表达式求值算法
这次来复习一下Dijkstra的双栈算术表达式求值算法,其实这就是一个计算器的实现,但是这里用到了不一样的算法,同时复习了栈. 主体思想就是将每次输入的字符和数字分别存储在两个栈中.每遇到一个单次结束 ...
- [BZOJ4011][HNOI2015] 落忆枫音(学习笔记) - 拓扑+DP
其实就是贴一下防止自己忘了,毕竟看了题解才做出来 Orz PoPoQQQ 原文链接 Description 背景太长了 给定一个DAG,和一对点(x, y), 在DAG中由x到y连一条有向边,求生成树 ...
- JavaScript中内存使用规则--堆和栈
堆和栈都是运行时内存中分配的一个数据区,因此也被称为堆区和栈区,但二者存储的数据类型和处理速度不同.堆(heap)用于复杂数据类型(引用类型)分配空间,例如数组对象.object对象:它是运行时动态分 ...
- [JCIP笔记](四)踩在巨人的肩上
读完第三章那些繁琐的术语和细节,头疼了整整一个星期.作者简直是苦口婆心,说得我如做梦一般.然而进入第四章,难度骤然降低,仿佛坐杭州的过山公交车突然下坡,鸟鸣花香扑面而来,看到了一片西湖美景. 从开始看 ...
- lvs 负载均衡 NAT模式
1.原理 基于NAT机制实现.当用户请求到达director之后,director将请求报文的目标地址(即VIP)改成选定的realserver地址,同时将报文的目标端口也改成选定的realserve ...
- Linux OpenGL 实践篇-6 光照
经典光照模型 经典光照模型通过单独计算光源成分得到综合光照效果,然后添加到物体表面特定点,这些成分包括:环境光.漫反射光.镜面光. 环境光:是指不是来特定方向的光,在经典光照模型中基本是个常量. 漫反 ...
- CSS3属性之圆角效果——border-radius属性
在css3之前,要实现圆角的效果可以通过图片或者用margin属性实现(可以参考这里:http://www.hicss.net/css-practise-of-image-round-box/).实现 ...