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. div 画园

    .destination1{ border: #666 solid 1px; box-shadow:-1px 1px 5px 0px #333; width:922px; height:485px; ...

  2. SQL Server 查询分析器键盘快捷方式

    下表列出 SQL Server 查询分析器提供的所有键盘快捷方式. 活动 快捷方式 书签:清除所有书签. CTRL-SHIFT-F2 书签:插入或删除书签(切换). CTRL+F2 书签:移动到下一个 ...

  3. Ubuntu-Python2.7安装 scipy,numpy,matplotlib 和pip

    一. scipy,numpy,matplotlib sudo apt-get install python-scipy sudo apt-get install python-numpy sudo a ...

  4. thinkphp 3.2

    ---恢复内容开始--- http://url.cn/ejCVUQ ---恢复内容结束---

  5. e577. Enabling Antialiasing

    // See e575 The Quintessential Drawing Program public void paint(Graphics g) { // Retrieve the graph ...

  6. C++/CLI中class成员声明与实现分开在不同文件时必须添加namespace

    以下是我的代码: //TaskConfigFile.h #pragma once using namespace System::Collections::Generic; using namespa ...

  7. python 脚本检测python 版本

    通过sys 模块的sys_info可以返回当前python 的版本信息, 其返回值是一个元组, 比如(2, 6, 6, 'final', 0); 表示当前版本为2.6.6 , 我们可以利用这个变量的值 ...

  8. JavaScript DOM 第3章 DOM

    3.4.5 获取元素 1. getElementById DOM提供了一个名为getElementById的方法,这个方法将返回一个与那个有着给定的id属性值的元素节点对应的对象. 文档中的每个元素都 ...

  9. 在hadoop集群添加了slave节点的方法

    分为以下几个步骤: 1  ,修改master和slave 的参数,和配置时相同,只是修改和节点数相关,如slaves(我的只改了slaves), 将任意一个该好的文件发送到新增加的机器(或者虚拟机) ...

  10. 在express项目中使用formidable & multiparty实现文件上传

    安装 formidable,multiparty 模块 npm install formidable,multiparty –save -d 表单上传 <form id="addFor ...