Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 29625   Accepted: 8034

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

Source

POJ Monthly,Zeyuan Zhu
 
题意:求s到t的第K短路

/*
*算法思想:
*单源点最短路径+高级搜索A*;
*A*算法结合了启发式方法和形式化方法;
*启发式方法通过充分利用图给出的信息来动态地做出决定而使搜索次数大大降低;
*形式化方法不利用图给出的信息,而仅通过数学的形式分析;
*
*算法通过一个估价函数f(h)来估计图中的当前点p到终点的距离,并由此决定它的搜索方向;
*当这条路径失败时,它会尝试其他路径;
*对于A*,估价函数=当前值+当前位置到终点的距离,即f(p)=g(p)+h(p),每次扩展估价函数值最小的一个;
*
*对于K短路算法来说,g(p)为当前从s到p所走的路径的长度;h(p)为点p到t的最短路的长度;
*f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;
*
*为了加速计算,h(p)需要在A*搜索之前进行预处理,只要将原图的所有边反向,再从终点t做一次单源点最短路径就能得到每个点的h(p)了;
*
*算法步骤:
*(1)将有向图转置即所有边反向,以原终点t为源点,求解t到所有点的最短距离;
*(2)新建一个优先队列,将源点s加入到队列中;
*(3)从优先级队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数;
*如果当前为t的第k次出队,则当前路径的长度就是s到t的第k短路的长度,算法结束;
*否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先级队列;
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
const int maxn=2e5+,maxm=2e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e17+;
struct edge
{
int from,to;
ll w;
};
vector<edge>G[maxn],T[maxn];
priority_queue<P,vector<P>,greater<P> >q;
ll dist[maxn];
void addedge(int u,int v,ll w)
{
G[u].push_back((edge)
{
u,v,w
});
T[v].push_back((edge)
{
v,u,w
});
}
void dij(int s)
{
dist[s]=0LL;
q.push(P(dist[s],s));
while(!q.empty())
{
P p=q.top();
q.pop();
int u=p.second;
for(int i=; i<T[u].size(); i++)
{
edge e=T[u][i];
if(dist[e.to]>dist[u]+e.w)
{
dist[e.to]=dist[u]+e.w;
q.push(P(dist[e.to],e.to));
}
}
}
}
struct node
{
int to;
///g(p)为当前从s到p所走的路径的长度;dist[p]为点p到t的最短路的长度;
ll g,f;///f=g+dist,f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;
bool operator<(const node &x ) const
{
if(x.f==f) return x.g<g;
return x.f<f;
}
};
ll A_star(int s,int t,int k)
{
if(dist[s]==INF) return -;
priority_queue<node>Q;
int cnt=;
if(s==t) k++;
ll g=0LL;
ll f=g+dist[s];
Q.push((node)
{
s, g, f
});
while(!Q.empty())
{
node x=Q.top();
Q.pop();
int u=x.to;
if(u==t) cnt++;
if(cnt==k) return x.g;
for(int i=; i<G[u].size(); i++)
{
edge e=G[u][i];
ll g=x.g+e.w;
ll f=g+dist[e.to];
Q.push((node)
{
e.to, g, f
});
}
}
return -;
}
void init(int n)
{
for(int i=; i<=n+; i++) G[i].clear(),T[i].clear();
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<=m; i++)
{
int u,v;
ll w;
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w);
}
int s,t,k;
scanf("%d%d%d",&s,&t,&k);
for(int i=; i<=n; i++) dist[i]=INF;
dij(t);
printf("%lld\n",A_star(s,t,k));
init(n);
return ;
}

第k短路

POJ 2449Remmarguts' Date 第K短路的更多相关文章

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

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

  2. poj 2449 Remmarguts' Date (k短路模板)

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

  3. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  4. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

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

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

  6. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

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

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

  8. POJ——2449Remmarguts' Date(A*+SPFA)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 26504   Accepted: 7203 ...

  9. POJ 2449 求第K短路

    第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...

随机推荐

  1. BOS物流项目第十三天

    教学计划 1.Quartz概述 a. Quartz介绍和下载 b. 入门案例 c. Quartz执行流程 d. cron表达式 2.在BOS项目中使用Quartz创建定时任务 3.在BOS项目中使用J ...

  2. lunux开放80端口(本地访问不了linux文件可能是这个原因)

    /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #开启80端口  /etc/rc.d/init.d/iptables save #保存配置  / ...

  3. Linux下开启计划任务日志

    crontab记录日志 修改rsyslog vim /etc/rsyslog.d/50-default.conf (我的是root用户) 搜索cron 把如下行之前的注释"#"去掉 ...

  4. express返回html文件

    [express返回html文件] app.engine(ext, callback) 方法即可创建一个你自己的模板引擎.其中,ext 指的是文件扩展名.callback 是模板引擎的主函数,接受文件 ...

  5. Kerberos 互信免登陆

    第一步:机器加互信 将机器A的Kerberos name加到机器B的~/.k5login中,同时将机器B的Kerberos name加到机器A的~/.k5login中 例如:host/bjm6-193 ...

  6. openvpn-admin(openvpn web管理 )

    openvpn 两种认证简介: 1.key分发: 在服务器端生成秘钥,然后下载到本地,将服务器端的ca.crt xx.crt xx.key ta.key(如果服务器启用的话需要,未开启的话不需要,功能 ...

  7. (转)学习ffmpeg官方示例transcoding.c遇到的问题和解决方法

    转自:https://blog.csdn.net/w_z_z_1991/article/details/53002416 Top 最近学习ffmpeg,官网提供的示例代码transcoding.c演示 ...

  8. 1、__del__ 2、item系列 3、__hash__ 4、__eq__

    1.__del__   析构方法       释放一个空间之前之前 垃圾回收机制   2.item系列   和对象使用[ ]访问值有联系 __getitem__ __setitem__ __delit ...

  9. Could not get lock /var/lib/dpkg/lock更新问题

    发生于apt-get update或apt update时. 常见手段,先试试: sudo rm /var/lib/dpkg/lock sudo rm /var/lib/apt/lists/lock ...

  10. Mysql的随机抽取

    方法一 SELECT * FROM SHARE ORDER BY RAND( ) LIMIT 1; 在MYSQL的官方手册,里面针对RAND()的提示大概意思就是,在ORDER BY从句里面不能使用R ...