Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 21549   Accepted: 5862

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
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 1009
#define eps 1e-10
#define inf 100000000
#define mod 100000000
#define INF 0x3f3f3f3f
using namespace std;
struct Lnode
{
int u,v,w,next;
}edge[M*300];
int t,head[M],h[M],num[M],use[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void dijstra(int n,int s)
{
int i,j;
memset(use,0,sizeof(use));
memset(h,INF,sizeof(h));
h[s]=0;
for(i=1;i<=n;i++)
{
int mini=INF;
int tep=-1;
for(j=1;j<=n;j++)
{
if(!use[j]&&h[j]<mini)
{
mini=h[j];
tep=j;
}
}
if(tep==-1)break;
use[tep]=1;
for(j=head[tep];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(h[v]>h[tep]+edge[j].w)
h[v]=h[tep]+edge[j].w;
}
}
}
struct node
{
int v,g,h;
friend bool operator<(node a,node b)
{
return a.g+a.h>b.g+b.h;
}
};
int bfs(int n,int s,int t,int k)
{
int i;
priority_queue<node>q;
memset(num,0,sizeof(num));
node cur;
cur.v=s;
cur.g=0;
cur.h=h[s];
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
num[cur.v]++;
if(num[cur.v]>k)continue;
if(num[t]==k&&t==cur.v)return cur.g;
for(i=head[cur.v];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
node now;
now.v=v;
now.g=cur.g+edge[i].w;
now.h=h[v];
q.push(now);
}
}
return -1;
}
struct line
{
int a,b,c;
}e[M*300];
int main()
{
int n,m,i,start,endl,k;
while(scanf("%d%d",&n,&m)!=-1)
{
init();
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
add(e[i].b,e[i].a,e[i].c);
}
scanf("%d%d%d",&start,&endl,&k);
if(start==endl)k++;//注意的地方
dijstra(n,endl);
init();
for(i=1;i<=m;i++)
add(e[i].a,e[i].b,e[i].c);
int ans=bfs(n,start,endl,k);
printf("%d\n",ans);
}
return 0;
}

第k最短路A*启发式搜索的更多相关文章

  1. K最短路 A*算法

    POJ2449 Remmarguts' Date #include <iostream> #include <algorithm> #include <queue> ...

  2. POJ 2449 Remmarguts' Date --K短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

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

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

  4. POJ--2449--Remmarguts&#39; Date【dijkstra_heap+A*】第K短路

    链接:http://poj.org/problem?id=2449 题意:告诉你有n个顶点,m条边.并把这些边的信息告诉你:起点.终点.权值.再告诉你s.t.k.需求出s到t的第k短路,没有则输出-1 ...

  5. 图的第k短路

    [问题描述] 给你一个有向图,求从1到n的第k短路. [解法] SPFA+A*搜索. 1 A*算法 A*算法在人工智能中是一种典型的启发式搜索算法,启发中的估价是用估价函数表示的: h(n)=f(n) ...

  6. A*算法的认识与求第K短路模板

    现在来了解A*算法是什么 现在来解决A*求K短路问题 在一个有权图中,从起点到终点最短的路径成为最短路,第2短的路成为次短路,第3短的路成为第3短路,依此类推,第k短的路成为第k短路.那么,第k短路怎 ...

  7. poj2449(k短路&A_star模板)

    题目链接:http://poj.org/problem?id=2449 题意:给出一个有向图,求s到t的第k短路: 思路:k短路模板题,可以用A_star模板过: 单源点最短路径+高级搜索A*;A*算 ...

  8. BZOJ-1975: 魔法猪学院 (K短路:A*+SPFA)

    题意:有N种化学元素,有M种转化关系,(u,v,L)表示化学物质由u变为v需要L能量,现在你有E能量,问最多有多少种不同的途径,使得1转为为N,且总能量不超过E. 思路:可以转为为带权有向图,即是求前 ...

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

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

随机推荐

  1. delete指针以后应赋值为NULL——QT deletelater指针以后也同样要马上赋值为NULL

    delete p后,只是释放了指针指向的内存空间.p并不会自动被置为NULL,而且指针还在,同时还指向了之前的地址 delete NULL编译器不会报错(因为delete空指针是合法的) 例: 对一个 ...

  2. WordCount示例深度学习MapReduce过程

    转自: http://blog.csdn.net/yczws1/article/details/21794873 . 我们都安装完Hadoop之后,按照一些案例先要跑一个WourdCount程序,来测 ...

  3. 标准的 C++ 由三个重要部分组成

    标准的 C++ 由三个重要部分组成: 核心语言,提供了所有构件块,包括变量.数据类型和常量,等等.C++ 标准库,提供了大量的函数,用于操作文件.字符串等.标准模板库(STL),提供了大量的方法,用于 ...

  4. struct iphdr中的__LITTLE_ENDIAN_BITFIELD和__BIG_ENDIAN_BITFIELD

    __LITTLE_ENDIAN_BITFIELD表示小端序,__BIG_ENDIAN_BITFIELD表示大端序. /usr/include/linux/ip.h中有一段代码定义了ip首部的结构体,例 ...

  5. 转载:MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解

    转自:http://www.jb51.net/article/39199.htm 本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍, ...

  6. MathTyp使用过程的几个问题

    最近毕业季,人们又开始了一波论文恐惧症了.每天都在不断地改来改去,格式还是不符合要求,头疼得要死.不仅如此,还发现公式是越改越乱,牵一发而全身,其它地方动一点,整个版面全都乱了,人都要抓狂了.知道你的 ...

  7. C#_获取汉字拼音

    using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...

  8. C语言文件路径中的”/“和“\“

    在不同系统的情况系   windows下是\,linux和unix下是/  但在win中没有本质区别. 但是由于   \   也是转义字符的起始字符,  所以, 路径中的   \   通常需要使用   ...

  9. python2.0_s12_day11_SqlAlchemy使用介绍

    SqlAlchemy ORM ORM的解释; 简单点:对象关系映射. 需求:我们写一个主机管理,把主机信息存在数据库,一开始我们编程不熟练的时候,执行命令时候要调用数据库,会把相应的SQL语句写到代码 ...

  10. array_diff 不注意的坑

    1)array_diff 是对比两个(或以上数组)的值的差集,注意是对比数组的值,和数组的键无关 2)是以第一个数组为对比对象,找上在第一个数组里有但其他数组里没有的值(可以同值但不同键的多个) 举个 ...