Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 33606   Accepted: 9116

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story.

"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 first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T.

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

Output

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

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短路 (最短路变形)的更多相关文章

  1. poj 2449 Remmarguts' Date (k短路模板)

    Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  2. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  3. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  4. POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )

    题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...

  5. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  6. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  7. POJ 2449 Remmarguts' Date (第k短路径)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:35025   Accepted: 9467 ...

  8. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  9. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

随机推荐

  1. js解决IE8不支持html5,css3的问题(respond.js 的使用注意)

    IE8.0及以下不支持html5,css3的解析.目前为止IE8以下的版本使用率在10%左右,网站还是有必要兼容的. 1,在你的所有css最后判断引入两个js文件. html5.js  是用来让ie8 ...

  2. 如何用UPA优化性能?先读懂这份报告!

    一.概述 打开一份UPA报告时,最先看到的就是概述页面,这也是我们推荐用户第一时间关注的页面.概述页面一开始会列出测试的基本信息,并根据腾讯游戏的性能标准,给出本次测试的结果(通过,不通过和警告): ...

  3. redis入门(05)redis的key命令

    一.什么是redis键命令 Redis 键(key):Redis 键命令用于管理 redis 的键. Redis 键命令的基本语法: redis 127.0.0.1:6379> COMMAND ...

  4. android studio 何如修改报名

    1. 重命名办法,网上很多见 2. 对于需要重新修改包名的级别的 a. 修改package 和 gradle 的包名,对应一致. b. 修改R 所在包名,使用crtl+n修改R文件的路径 c. 手动首 ...

  5. javascript学习总结一

    1. 变量提升hoisting 变量提升的意思是在一个变量作用域里定义的变量的声明会被提升到作用域的顶部,这是变量只会被声明,不会被初始化复制,而是undefined. 代码如下: function ...

  6. POJ-1125 Stockbroker Grapevine---Floyd应用

    题目链接: https://vjudge.net/problem/POJ-1125 题目大意: 股票经纪人要在一群人中散布一个谣言,而谣言只能在亲密的人中传递,题目各处了人与人之间的关系及传递谣言所用 ...

  7. Windows下安装Python3和Django

    下载python3 首先去 python的官网 下载最新稳定版的python3, 我下载的时候python3的最新版本是3.6.5. 亦可点击 此链接 直接下载. 安装python3 傻瓜式安装,注意 ...

  8. [原创软件]Maya语言切换工具

    软件主要功能: 切换Maya语言 软件界面截图: 开发环境及语言: c# .NET Framework 4.0 Visual Studio 2015 更新日志: v1.0(2016.7.20) 发布初 ...

  9. 初探java对象比较

    判断两个对象的属性值是否相等的方法, class Book{ private String title; private double price; public Book(String title, ...

  10. 使用 Cesium 动态加载 GeoJSON 数据

    前言 需求是这样的,我需要在地图中显示 08 年到现在的地震情况,地震都是发生在具体的时间点的,那么问题就来了,如何实现地震情况按照时间动态渲染而不是一次全部加载出来. 一. 方案分析 这里面牵扯到两 ...