【题目链接】

http://poj.org/problem?id=2449

【算法】

A*(启发式搜索)

首先,求第k短路可以用优先队列BFS实现,当T第k次入队时,就求得了第k短路,但是,这种做法的复杂度太高

考虑使用A*算法,每个点的估价函数就是这个点到T的最短路,不妨将所有的边反过来求最短路,这样就得到了所有点的估价函数

这种算法的复杂度是优秀的

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1010
#define MAXM 100010
const int INF = 2e9; int i,n,m,tot,rtot,u,v,w,S,T,K;
int head[MAXN],rhead[MAXN],dist[MAXN]; struct Edge
{
int to,w,nxt;
} e[MAXM<<];
struct info
{
int s,d;
friend bool operator < (info a,info b)
{
return a.d + dist[a.s] > b.d + dist[b.s];
}
} ; inline void add(int u,int v,int w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
tot++;
e[tot] = (Edge){u,w,rhead[v]};
rhead[v] = tot;
}
inline void dijkstra(int s)
{
int i,v,w;
priority_queue< pair<int,int> > q;
pair<int,int> cur;
static bool visited[MAXN];
while (!q.empty()) q.pop();
for (i = ; i <= n; i++)
{
dist[i] = INF;
visited[i] = false;
}
dist[s] = ;
q.push(make_pair(,s));
while (!q.empty())
{
cur = q.top();
q.pop();
if (visited[cur.second]) continue;
visited[cur.second] = true;
for (i = rhead[cur.second]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (dist[cur.second] + w < dist[v])
{
dist[v] = dist[cur.second] + w;
q.push(make_pair(-dist[v],v));
}
}
}
}
inline int Astar(int s,int t,int k)
{
int i,cnt = ,v,w;
priority_queue< info > q;
info cur;
while (!q.empty()) q.pop();
q.push((info){s,});
while (!q.empty())
{
cur = q.top();
q.pop();
if (cur.s == t)
{
cnt++;
if (cnt == k) return cur.d;
}
for (i = head[cur.s]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
q.push((info){v,cur.d+w});
}
}
return -;
} int main()
{ while (scanf("%d%d",&n,&m) != EOF)
{
tot = rtot = ;
for (i = ; i <= n; i++) head[i] = rhead[i] = ;
for (i = ; i <= m; i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
scanf("%d%d%d",&S,&T,&K);
if (S == T) K++;
dijkstra(T);
printf("%d\n",Astar(S,T,K));
} return ; }

【POJ 2449】 Remmarguts' Date的更多相关文章

  1. 【POJ】【2449】Remmarguts' Date

    K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...

  2. POJ 2449:Remmarguts' Date(A* + SPFA)

    题目链接 题意 给出n个点m条有向边,源点s,汇点t,k.问s到t的第k短路的路径长度是多少,不存在输出-1. 思路 A*算法是启发式搜索,通过一个估价函数 f(p) = g(p) + h(p) ,其 ...

  3. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  4. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  5. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  6. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  7. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  8. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  9. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

随机推荐

  1. 微信小程序 "request:fail 发生了 SSL 错误无法建立与该服务器的安全连接。"

    android机子可以真机预览,ios机子报这个错误 检测域名 苹果ATS检测 https://cloud.tencent.com/product/ssl#userDefined10 以上都通过 ht ...

  2. MyEclipse快捷键 (精简)

    在调试程序的时候,我们经常需要注释一些代码,在用Myeclipse编程时,就可以用 Ctrl+/ 为选中的一段代码加上以 // 打头的注释:当需要恢复代码功能的时候,又可以用Ctrl+/ 去掉注释.这 ...

  3. HDU_2203_KMP入门

    亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. ubuntu下Fiddler抓包

    参考 https://www.cnblogs.com/jcli/p/4474332.html https://www.jianshu.com/p/4505c732e378 1. 你要有个Mono环境, ...

  5. java静态变量、实例变量和局部变

    实例变量又称成员变量: 1⃣️成员变量定义在类中,在整个类中都可以被访问 2⃣️成员变量随着对象的建立而建立,随对象的消失而消失,存在于对象所在的对内存中 3⃣️成员变量有默认初始值 局部变量: 1⃣ ...

  6. docker和jenkins安装启动

    docker安装Jenkins 1.安装Docker 1.1 yum 包更新到最新 sudo yum update 1.2 安装需要的软件包, yum-util 提供yum-config-manage ...

  7. codevs1231 最优布线问题

    1231 最优布线问题 题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用可能是不同的.为了节省费用,我们考虑采用间接数据传输结束,就是一台计算机可以间接地 ...

  8. 面向对象:classmethod、staticmethod、property

    一.classmethod(类方法).staticmethod(静态方法) 方法包括:普通方法.类方法和静态方法,三种方法在内存中都归属于类,区别在于调用方式不同. # 普通方法 由对象调用,至少一个 ...

  9. 《零压力学Python》 之 第一章知识点归纳

    第一章(初识Python)知识点归纳 Python是从ABC语言衍生而来的 ABC语言是Guido参与设计的一种教学语言,为非专业编程人员所开发的. Python是荷兰程序员 Guido Van Ro ...

  10. http长链接问题

    http://www.ibm.com/developerworks/cn/web/wa-lo-comet/