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. nodejs 全局变量

    1.全局对象 所有模块都可以调用 1)global:表示Node所在的全局环境,类似于浏览器中的window对象. 2)process:指向Node内置的process模块,允许开发者与当前进程互动. ...

  2. 新概念英语(1-109)A Good Idea

    Lesson 109 A good idea 好主意 Listen to the tape then answer this question. What does Jane have with he ...

  3. Java设计模式(八)Proxy代理模式

    一.场景描述 代理在生活中并不少见,租房子需要找中介,打官司需要找律师,很多事情我们需要找专业人士代理我们做,另一方面,中介和律师也代理了房东.法律程序与我们打交道. 当然,设计模式中的代理与广义的代 ...

  4. jsp连接书库DatabaseUtil类

    public class DatabaseUtil { private static String driver = ConfigManager.getProperties("driver& ...

  5. c#:实现动态编译,并实现动态MultiProcess功能(来自python multiprocess的想法)

    由于之前一直遇到一些关于并行进行数据处理的时效果往往不好,不管是c#还是java程序都是一样,但是在Python中通过multiprocess实现同样的功能时,却发现确实可以提高程序运行的性能,及服务 ...

  6. type="file"实现兼容IE8本地选择图片预览

    一.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Uploa ...

  7. mysql zip 文件安装

    1.下载 https://dev.mysql.com/downloads/mysql/ 2.解压,配置环境变量 MYSQL_HOME:D:\mysql path后面加 :%MYSQL_HOME%\bi ...

  8. 一、spring的成长之路——代理设计模式

    java常用的设计模式详解: 1.代理模式(JDK的动态代理) [IDept.java] ​ 这是一个简单的就接口,进行数据的更新 package com.itcloud.pattern.proxy; ...

  9. 实验吧_貌似有点难(php代码审计)&头有点大

    二话不说先贴代码 <?php function GetIP(){ if(!empty($_SERVER["HTTP_CLIENT_IP"])) $cip = $_SERVER ...

  10. Java IO(二)

    上一节我们说到通过File访问文件,但是我们访问文件的最终目的都是访问文件中的内容,那么这个时候我们就需要使用到的一个类就是:RandomAccessFile. 此类的定义如下: public cla ...