Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 30772   Accepted: 8397

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短路
A*
估价函数为dis(起点,i)+dis(i,终点) 1、反向图上求出终点到每个点的最短路
2、起点入优先队列,
  队首出队,
  如果队首是终点,而且是第k次出队,
那么当前距离就是k短路
如果队首不是终点,便利与当前点连接的所有的点,入队 细节1:优先队列出入队不用vis数组判重,因为边可以重复走
细节2:如果第1步中,起点与终点不连通,输出-1结束,
    否则进入A*,没有vis数组,出现环会死循环
细节3:如果起点=终点,令k++,因为起点会立即出队
#include<queue>
#include<cstdio>
#include<cstring>
#define N 1001
#define M 100001
using namespace std;
int n,s,t,k;
int dis1[N];
bool vis[N];
int front[N],to[M],nxt[M],val[M],tot;
int front2[N],to2[M],nxt2[M],val2[M],tot2;
struct node
{
int num,dis;
bool operator < (node p) const
{
return dis+dis1[num]>p.dis+dis1[p.num];
}
}now,nt;
void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w;
to2[++tot2]=u; nxt2[tot2]=front2[v]; front2[v]=tot2; val2[tot2]=w;
}
void init()
{
int m,u,v,w;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
scanf("%d%d%d",&s,&t,&k);
}
void spfa()
{
memset(dis1,,sizeof(dis1));
queue<int>q;
dis1[t]=;
vis[t]=true;
q.push(t);
int now;
while(!q.empty())
{
now=q.front();
q.pop();
vis[now]=false;
for(int i=front2[now];i;i=nxt2[i])
if(dis1[to2[i]]>dis1[now]+val2[i])
{
dis1[to2[i]]=dis1[now]+val2[i];
if(!vis[to2[i]])
{
q.push(to2[i]);
vis[to2[i]]=true;
}
}
}
}
void Astar()
{
if(dis1[s]>1e9)
{
printf("-1");
return;
}
if(s==t) k++;
int cnt=,last=-;
priority_queue<node>q;
now.num=s;
now.dis=;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
if(now.num==t)
{
cnt++;
if(cnt==k)
{
printf("%d",now.dis);
return;
}
}
for(int i=front[now.num];i;i=nxt[i])
{
nt.num=to[i];
nt.dis=now.dis+val[i];
q.push(nt);
}
}
printf("-1");
}
int main()
{
init();
spfa();
Astar();
}
												

poj 2449 Remmarguts' Date (k短路模板)的更多相关文章

  1. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

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

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

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

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

  4. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  5. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

  6. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  7. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  8. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  9. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

随机推荐

  1. php面试全套

    7.mvc是什么?相互间有什么关系? 答:mvc是一种开发模式,主要分为三部分:m(model),也就是模型,负责数据的操作;v(view),也就是视图,负责前后台的显示;c(controller), ...

  2. “Hello world!”团队第一周贡献分分配结果

    小组名称:Hello World! 项目名称:空天猎 组长:陈建宇 成员:刘成志.阚博文.刘淑霞.黄泽宇.方铭.贾男男 第一周贡献分分配结果   基础分 会议分 提功能分 个人表现分 各项总分 最终分 ...

  3. c# 计算两个时间的时间差

    //计算2个日期之间的天数差 DateTime dt1 = Convert.ToDateTime("2007-8-1"); DateTime dt2 = Convert.ToDat ...

  4. Java中I/O流之处理流类型

    节点流:一个管道直接连接到数据源上面: 处理流:套在别的管道上面的管道: 处理流类型: [注]:在字符流中的OuPutStreamReader写错了,应该是:OutputStreamWriter

  5. DDB与DIB

    DB与DIB的区别是什么?觉得书上介绍的有点抽象.不容易理解.他们两者之间的区别的“物理意义” [“现实意义”]——姑且这么叫吧,呵呵!被这个问题困扰了很久,所以今天决定好好查资料总结一下,把它彻底搞 ...

  6. phpcms V9如何判断用户是否登录以及登陆后的标签写法问题

    首先要获取userid {php$userid=param::get_cookie('_userid');}​ 然后再判断是否为空 {if $userid}...这里写已经登录之后的代码...{els ...

  7. [剑指Offer] 47.求1+2+3+...+n

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). [思路]用&&的短路思想来求和 ...

  8. DELPHI Showmodal 模式窗体

    Showmodal 是个函数, Show 是个过程 1.     Showmodal: 概念 : 当你调用一个窗口用 SHOWMODAL 时 , 当这个窗口显示出来后 , 程序不会继续自己执行 , 而 ...

  9. java邮件开发

    一.邮件协议: (重点)SMTP:发送邮件的协议.Simple Message Transfer Protocal.默认端口:25 POP:邮局协议(收件协议).Post Office Protoca ...

  10. 获取接口参数名带有“abc”的参数的值

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) va ...