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!& ...
随机推荐
- 使用location.href跳转页面在火狐浏览器中报错404
HTML文件中引入外部js文件,在该js文件里用window.location.href跳转相对路径下的html地址,火狐浏览器会报错404,而谷歌浏览器却显示正常·,分析了一下原因:在识别相对路径时 ...
- WPF 自定义DataGrid控件样式
内容转自https://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.html 一.DataGrid基本样式(一) 小刚已经把Dat ...
- ELK学习总结(3-3)elk的组合查询
1.bool 查询: must: 必须 should: 可以满足,也可以不满足. must_not: minimum_should_match: 至少要x个匹配才算匹配成功 disable_coor ...
- cv2.cornerHarris()详解 python+OpenCV 中的 Harris 角点检测
参考文献----------OpenCV-Python-Toturial-中文版.pdf 参考博客----------http://www.bubuko.com/infodetail-2498014. ...
- MySQL命令(逐步更新ing)
启动mysql 开启: /etc/init.d/mysqld start关闭: /etc/init.d/mysqld stop重启: /etc/init.d/mysqld restart 查看m ...
- linux添加硬盘分区挂载教程
基本步骤:分区--格式化--挂载--写入文件 1.首先用fdisk -l命令查看添加的硬盘名称,可以看到sdb为新增的硬盘 [root@oracle ~]# fdisk -l Disk /dev/sd ...
- scrapy选择器主要用法
# 命令行输入:scrapy shell +链接,会自动请求url,得到的相应默认为response,开启命令行交互模式 scrapy shell http://doc.scrapy.org/en/l ...
- Zookeeper增删改查
1.下载Zookeeper http://mirrors.shu.edu.cn/apache/zookeeper/ 这里我选择Zookeeper 3.4.11版本 ZooKeeper 支持某些特定的四 ...
- 【MySQL】通过Binary Log简单实现数据回滚(一)
一.前言 对,没错,我又水了好一阵子,深刻反思寄几.前段时间,工作项目上出于对excel等批量操作可能出现误操作的问题,要求提供一个能够根据操作批次进行数据回滚的能力.在开发的过程中接触到了MySQL ...
- 逻辑运算符、三元运算符、for循环、stack(栈),heap(堆),方法区,静态域
Lesson One 2018-04-17 19:58:39 逻辑运算符(用于逻辑运算,左右两边都是 true 或 false) 逻辑与-& 和 短路与-&& 区别: & ...