k短路模板
https://acm.taifua.com/archives/jsk31445.html
链接:
https://nanti.jisuanke.com/t/31445
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
int n, m, s, t, k, Time;
bool vis[maxn];
int dist[maxn]; struct Node
{
int v, c;
Node (int _v = , int _c = ): v(_v), c(_c) {}
bool operator < (const Node &rhs) const
{
return c + dist[v] > rhs.c + dist[rhs.v]; // 估价函数 fx = gx + hx 路径短先出队
}
}; struct Edge
{
int to, cost;
Edge (int _to = , int _cost = ): to(_to), cost(_cost) {}
}; vector <Edge> E[maxn], revE[maxn]; void addedge(int u, int v, int w)
{
E[v].push_back(Edge(u, w)); // 反向加边
revE[u].push_back(Edge(v, w)); // 正向加边
} void dijkstra(int s, int n) // 最短路
{
for (int i = ; i <= n; ++i)
{
vis[i] = false;
dist[i] = inf;
}
dist[s] = ;
priority_queue <Node> Q;
Q.push(Node(s, dist[s]));
while (!Q.empty())
{
Node tmp = Q.top();
Q.pop();
int u = tmp.v;
if (vis[u])
continue;
vis[u] = true;
for (int i = ; i < E[u].size(); ++i)
{
int v = E[u][i].to, cost = E[u][i].cost;
if (!vis[v] && dist[v] > dist[u] + cost)
{
dist[v] = dist[u] + cost;
Q.push(Node(v, dist[v]));
}
}
}
} int astar(int s)
{
if (dist[s] == inf) // 不能到达
return -;
priority_queue <Node> Q;
Q.push(Node(s, ));
k--;
while (!Q.empty())
{
Node tmp = Q.top();
Q.pop();
int u = tmp.v;
if (tmp.c + dist[u] > Time) // 超时直接返回-1,可不要
return -;
if (u == t)
{
if (k)
--k;
else // 第k次到达目标节点t
return tmp.c;
}
for (int i = ; i < revE[u].size(); ++i)
{
int v = revE[u][i].to, cost = revE[u][i].cost;
Q.push(Node(v, tmp.c + cost));
}
}
return -;
} int main()
{
int u, v, w;
while (scanf("%d%d", &n, &m) != EOF)
{
scanf("%d%d%d%d", &s, &t, &k, &Time);
for (int i = ; i <= n; ++i)
{
E[i].clear();
revE[i].clear();
}
for (int i = ; i < m; ++i)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
dijkstra(t, n); // t点到所有点的最短路
int ans = astar(s);
if (ans == - || ans > Time) // 无法到达或者超过时间
printf("Whitesnake!\n");
else
printf("yareyaredawa\n");
}
return ;
}
k短路模板的更多相关文章
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- k短路模板 POJ2449
采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(第k短路模板)
求第k短路模板 先逆向求每个点到终点的距离,再用dij算法,不会超时(虽然还没搞明白为啥... #include<iostream> #include<cstdio> #inc ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- poj 2499第K短路模板
第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- 【Luogu】P2901牛慢跑(K短路模板)
题目链接 K短路居然用A*……奇妙. 先建反图从终点(1)跑一遍最短路,再A*,用堆存当前点到终点距离+从起点到当前点距离. 每次取出终点都可以视为发现了一个新的最短路. #include<cs ...
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- 第K短路模板【POJ2449 / 洛谷2483 / BZOJ1975 / HDU6181】
1.到底如何求k短路的? 我们考虑,要求k短路,要先求出最短路/次短路/第三短路……/第(k-1)短路,然后访问到第k短路. 接下来的方法就是如此操作的. 2.f(x)的意义? 我们得到的f(x)更小 ...
随机推荐
- 059 Spiral Matrix II 旋转打印矩阵 II
给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...
- java.lang.UnsupportedOperationException: setXIncludeAware is not supported on this JAXP implementation or earlier: class gnu.xml.dom.JAXPFactory的解决办法(图文详解)
不多说,直接上干货! 问题详情 java.lang.UnsupportedOperationException: setXIncludeAware is not supported on this J ...
- IIS访问网站出错[要求输入用户名密码]的解决方案
症状: 1.HTTP 500 - 内部服务器错误 2.您不具备使用所提供的凭据查看该目录或页的权限 3.基于所提供的凭据,您没有权限查看此目录或网页.HTTP 错误 401.3 - 访问被资源 ACL ...
- sql常用操作(三)多表查询
1 连接查询 1.1连接就是指两个或2个以上的表(数据源)“连接起来成为一个数据源”. 实际上,两个表的完全的连接是这样的一个过程: 左边的表的每一行,跟右边的表的每一行,两两互相“横向对接”后所得到 ...
- 常用API(正则表达式、Date、DateFormat、Calendar)
常用API 今日内容介绍 u 正则表达式 u Date u DateFormat u Calendar 第1章 正则表达式 1.1 正则表达式的概念 正则表达式(英语:Regular Expressi ...
- macOS Sierra 最新系统找回允许任何软件安装
终端输入就可以了 安装macOS Sierra后,会发现系统偏好设置的“安全与隐私”中默认已经去除了允许“任何来源”App的选项,无法运行一些第三方应用. 如果需要恢复允许“任何来源”的选项,即关闭G ...
- Failed to crunch file
Failed to crunch file 编译时,出现以上错误,经过多次排除验证,原因尽然是因为路径字符太长了... 编译路径不能超过240个字符
- 本号讯 | 微软和百度携手推进全球自动驾驶技术; 微软发布新一代可垂直可水平滚动的Arc鼠标
7 月 13 日,微软宣布了与宝马的最新合作进展,继语音助手 Cortana .云服务 Azure.Office 365 和微软 Exchange 安装在部分宝马车型后——Skype for Busi ...
- 在Office 365 添加就地保留用户邮箱
基于客户需求,要求将用户批量添加到Office 365中的现有就地保留.如您所了解的,我们可以通过Exchange在线图形用户GUI界面完成,也可以通过PowerShell完成. 要将用户批量添加到O ...
- Mandelbrot图像
using System;using System.Collections.Generic;using System.Text; namespace ConsoleApplication3{ ...