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!& ...
随机推荐
- 深度学习之 GAN 进行 mnist 图片的生成
深度学习之 GAN 进行 mnist 图片的生成 mport numpy as np import os import codecs import torch from PIL import Imag ...
- maven多环境部署
1.首先在maven的pom.xml文件中添加profiles标签,然后分别添加3个不同环境的profile, 本例中添加了dev.test.product三个环境,这个可以根据自己的需要添加和减少. ...
- 回收 PV - 每天5分钟玩转 Docker 容器技术(152)
当 PV 不再需要时,可通过删除 PVC 回收. 当 PVC mypvc1 被删除后,我们发现 Kubernetes 启动了一个新 Pod recycler-for-mypv1,这个 Pod 的作用就 ...
- Spring Security 入门(1-6-2)Spring Security - 内置的filter顺序、自定义filter、http元素和对应的filterChain
Spring Security 的底层是通过一系列的 Filter 来管理的,每个 Filter 都有其自身的功能,而且各个 Filter 在功能上还有关联关系,所以它们的顺序也是非常重要的. 1.S ...
- MySql入门(2-2)创建数据库
mysql -u root -p; show databases; create database apigateway; use apigateway; show tables;
- Tesseract-OCR4.0识别中文与训练字库实例
关于中文的识别,效果比较好而且开源的应该就是Tesseract-OCR了,所以自己亲身试用一下,分享到博客让有同样兴趣的人少走弯路. 文中所用到的身份证图片资源是百度找的,如有侵权可联系我删除. 一. ...
- python中 functools模块 闭包的两个好朋友partial偏函数和wraps包裹
前一段时间学习了python当中的装饰器,主要利用了闭包的原理.后来呢,又见到了python当中的functools模块,里面有很多实用的功能.今天我想分享一下跟装饰器息息相关的两个函数partial ...
- win10下安装Ubuntu16.04双系统
其实我是不喜欢系统的,之前都是在win下面进行开发,现在来了个项目,经过各种环境的安装调研,最终选择在Ubuntu下面进行开发.之前想着为啥不在虚拟机里面安装Ubuntu进行操作呢,由于虚拟机的体验不 ...
- Linux命令-权限
Linux命令权限 1.新建用户natasha,uid为1000, gid为555, 备注信息为"master" 2.修改natasha用户的家目录为/Natasha 3.查看 ...
- JavaScript的数组实现队列与堆栈的方法
一.队列和堆栈的简单介绍 1.1.队列的基本概念 队列:是一种支持先进先出(FIFO)的集合,即先被插入的数据,先被取出! 如下图所示: 1.2.堆栈的基本概念 堆栈:是一种支持后进先出(LIFO)的 ...