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. 纯CSS实现圆角、可拖动的一个DIV模块层

    <style>body{ margin:0px; padding:0px; font-size:14px;}#t { position:absolute; float:left; left ...

  2. 试读《基于MVC的JavaScript Web富应用开发》— 不一样的JavaScript

    前言 <基于MVC的JavaScript Web富应用开发>是ItEye在7月份发起试读的书.下载了试读的章节,发现只有全本的开始到第二章,第一章很简洁明了地讲述了JavaScript的历 ...

  3. Windoows窗口程序二

    WNDCLASS属性style取值: CS_GLOBALCLASS--应用程序全局窗口类 CS_BYTEALIGNCLIENT--窗口客户区的水平位置8倍数对齐 CS_BYTEALIGNWINDOW- ...

  4. HBase二级索引方案总结

    转自:http://blog.sina.com.cn/s/blog_4a1f59bf01018apd.html 附hbase如何创建二级索引以及创建二级索引实例:http://www.aboutyun ...

  5. C++ 继承、函数重载

    题外话1:浪费了两天,可耻! 题外话2:你这个年纪,做得好是理所当然,做不好是罪孽深重!!! --- 深以为然. 题外话3:从开始看C++ Primer 到现在,整整24天了,没想到基础方面耗费这么久 ...

  6. 关于VS2012连接MySql数据库时无法选择数据源

    您的C#开发工具是用VS2012吗?    No! return;    您的数据库用的是MySql吗?     No! return;    您新建ADO.NET数据实体模型的时候选择数据源的时候没 ...

  7. chrome 版本升级到56,报错Your connection is not private NET::ERR_CERT_WEAK_SIGNATURE_ALGORITHM

    ubuntu14.04 解决方法: $ sudo apt-get install libnss3-1d ref: http://stackoverflow.com/questions/42085055 ...

  8. More is better-多多益善

    思路:在图中所有的连通分量中找出包含顶点最多的个数.继续使用并查集解决! #include <iostream> using namespace std; ; int tree[MAX]; ...

  9. 今天搞log4net插入错误日志去mysql数据库的时候出现了点问题,已解决。记录下解决方案

    先上图 配置log4net的时候要填这项,可是这个value我不知道啊.....上图里的value是我用下面的方法获取的 MySqlConnection con = new MySqlConnecti ...

  10. 刚刚完成了在vs2013中通过 ef连接mysql数据库的工作。感觉没有想象中的简单。试了n次终于成功。故记录成功的方法,希望可以帮到大家

    分两种情况,如果你是用entity framework 5.0的时候 mysql-connector-net的版本不是很重要. MySQL For VisualStudio的版本也不重要 (这个不装就 ...