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)更小 ...
随机推荐
- JavaSE---悲观锁与乐观锁
1.[悲观锁] 1.1 在数据处理的整个过程中,数据将处于锁定状态: 1.2 悲观锁的实现,依赖于数据库提供的锁机制(只有数据库提供的锁机制才能真正保证数据访问的排他性,否则,即使在系统中加锁机制,也 ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- 日志AOP的实现
/// <summary> /// 日志AOP拦截 /// </summary> [AttributeUsage(AttributeTargets.Method, AllowM ...
- vue.js数据绑定语法
原始高清大图下载 1.数据绑定 html代码: <div id="first" class="first">msg:{{msg}}</div& ...
- SpringMVC简介01
SpringMVC也叫Spring Web mvc,属于表现层的框架.SpringMVC是Spring框架的一部分,是在Spring3.0后发布的. Spring结构图: SpringMVC架构: S ...
- MyBatis学习总结(二)---实例
为了对MyBatis有个初步了解,现做一个简单的增.删.改.查实例.了解涉及的文件与相关作用. MySql创建friends表,以下是表的sql语句 DROP TABLE IF EXISTS `fri ...
- C#、VSTO讀取Excel類
之前寫的類存在Excel進程不能結束的Bug,重寫ExcelReader類,類實例清理時Excel進程自動結束. class ExcelReader { // Excel Object public ...
- vue-cli3项目优化首页加载过慢的一些心得
博主最近发现vue-cli3项目做完后,点击首页加载时间好久啊,一般都要3-5s.这样的加载时间博主自己都受不了,所以就有了这个随笔,将自己的一些研究心得分享给大家. 首先推荐大家下载一个webpac ...
- JavaScript中函数对象和对象的区别
function Test (word) { console.log (word); } Test('哈哈,我是函数'); new Test('哈哈,我是对象'); //将以上的调用方式换种通俗易懂的 ...
- IE浏览器已经卸载,但是桌面上的图标却无法删除的解决方案
第一步——win+R运行[regedit],打开注册表. 第二步——在注册表里面依次找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVer ...