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)更小 ...
随机推荐
- NET Core 开发环境
NET Core 开发环境 最近,一直在往.Net Core上迁移,随着工作的深入,发现.Net Core比.Net Framework好玩多了.不过目前还在windows下开发,虽然VisualSt ...
- postgresql修改数据库名
alter database abc rename to cba;
- vue学习之路之需要了解的知识汇总
一.vue是什么? 相关网页: https://vuejs.bootcss.com/v2/guide/ 及菜鸟教程 https://www.runoob.com/vue2/v ...
- css position 定位模式
定位 定位模式: static relative absolute fixed 边偏移 :top bottom left right 一般的定位必须要有定位模式以及边偏移 static 静态定位 默 ...
- mysql 5.7.20 在线安装与卸载(yum卸载与rpm卸载方式)
mysql5.7.20和之前的5.7.16版本不同,解压后没有data文件,需要自己建立 1.把下载的mysql5.7.20放到目录:/usr/local/2.卸载cenos上预装的mysql查看已安 ...
- Java transient关键字使用
1. transient的作用及其使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的 ...
- SQL Server扩展事件system_health会话总结
system_health会话概念 我们知道扩展事件(Extended Events)是从SQL Server 2008开始引入的.system_health会话是SQL Server默认包含的扩展事 ...
- 微软Coco Blockchain Framework:一键解决企业级区块链三大难题
近年来,异军突起的“区块链”受到全行业的广泛关注,众多企业级用户在积极拥抱新技术的过程中却面临三大难题:性能.隐私和组织管理.如果不能很好地解决这些“顽固分子”,区块链技术就相对局限,很难发挥出应有的 ...
- web端 repeat和简单控件
<%@ %> - 这里面写一些声明和引用的<% %> - 编写C#代码的<%= %><%# %> Repeater - 重复器 相当于winfo ...
- jsp另外五大内置对象之response-操作cookie
responseo3.jsp <%@ page language="java" contentType="text/html; charset=utf-8" ...