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. [oracle] 两种权限:系统权限VS对象权限

    系统权限表示对表和表空间等   有无操作权  的权限.一般是SYS用户这种DBA来授权.比如: grant create session to lisi grant create table to l ...

  2. tomcat安装不成功.提示:failed to install tomcat6 service ,check your setting and permissions

    这个问题主要是因为旧版本卸载不完全导致的,可通过彻底删除旧版本解决,方案如下: 以管理员身份运行 命令提示符,弹出窗口 ,选择“是”,输入 sc delete tomcat5 ,或者 sc delet ...

  3. matplotlib中的legend()——用于显示图例

    legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import matplotlib.pyplot as plt import numpy ...

  4. CentOS7设置开机自启动命令大全

    任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd  on              systemctl enable httpd.service 使某服务不自 ...

  5. CentOS系统中的passwd命令实用技巧小结

    这篇文章主要介绍了Linux系统中的passwd命令实用技巧小结,是Linux入门学习中的基础知识,需要的朋友可以参考下   先来回顾一下passwd命令的基本用法: Linux passwd命令用来 ...

  6. Spring-Resource接口

    4.1.1 概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源资源.ClassPath相关资源.服务器相关资源(JBoss AS 5.x上的VFS资源)等等很 ...

  7. jQuery动态生成Bootstrap表格

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

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

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

  9. VMWare虚拟机提示:打不开磁盘…或它所依赖的某个快照磁盘,开启模块DiskEarly的操作失败,未能启动虚拟机

    将电脑上存在的虚拟机复制一份后打开运行,弹出错误提示: 打不开磁盘…或它所依赖的某个快照磁盘,开启模块DiskEarly的操作失败,未能启动虚拟机. 解决方法如下: 打开存放虚拟机系统硬盘的所在文件夹 ...

  10. Linux 下安装 Python3

    Linux CentOS 7 安装 Python3: [root@localhost ~]$ yum install -y epel-release [root@localhost ~]$ yum i ...