【luogu P4568 [JLOI2011]飞行路线】 题解
题目链接:https://www.luogu.org/problemnew/show/P4568
卡了一晚上,算是分层图最短路的模板。注意卡SPFA,所以我写了个SLF优化。
同时 AC400祭!~
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ri register
using namespace std;
const int maxn = 200000 + 10;
const int inf = 2147483647;
inline int read()
{
int k=0,f=1;
char c=getchar();
while(!isdigit(c))
{
if(c=='-')f=-1;
c=getchar();
}
while(isdigit(c))
{
k=(k<<1)+(k<<3)+c-48;
c=getchar();
}
return k*f;
}
int n, m, k, s, end, dis[maxn][20];
bool vis[maxn][20];
struct edge{
int from, len, to, next;
}e[maxn<<2];
int head[maxn], cnt = 0;
struct que{
int a, b;
};
deque<que> q;
inline void add(int u, int v, int w)
{
e[++cnt].from = u;
e[cnt].to = v;
e[cnt].len = w;
e[cnt].next = head[u];
head[u] = cnt;
}
inline void SPFA()
{
memset(dis, 127, sizeof(dis));
memset(vis, 0, sizeof(vis));
q.push_back((que){s,0});
dis[s][0] = 0;
vis[s][0] = 1;
while(!q.empty())
{
que now = q.front(); q.pop_front();
vis[now.a][now.b] = 0;
for(ri int i = head[now.a]; i != -1; i = e[i].next)
{
if(dis[e[i].to][now.b] > dis[now.a][now.b] + e[i].len)
{
dis[e[i].to][now.b] = dis[now.a][now.b] + e[i].len;
if(vis[e[i].to][now.b] == 0)
{
vis[e[i].to][now.b] = 1;
if(q.empty() || dis[e[i].to][now.b] > dis[q.front().a][now.b])
q.push_back((que){e[i].to, now.b});
else
q.push_front((que){e[i].to, now.b});
}
}
if(now.b + 1 <= k)
{
if(dis[e[i].to][now.b + 1] > dis[now.a][now.b])
{
dis[e[i].to][now.b + 1] = dis[now.a][now.b];
if(vis[e[i].to][now.b + 1] == 0)
{
vis[e[i].to][now.b + 1] = 1;
if(q.empty() || dis[e[i].to][now.b] > dis[q.front().a][now.b + 1])
q.push_back((que){e[i].to, now.b + 1});
else
q.push_front((que){e[i].to, now.b + 1});
}
}
}
}
}
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
memset(head, -1, sizeof(head));
n = read(); m = read(); k = read(); s = read(); end = read();
for(ri int i = 1; i <= m; i++)
{
int u, v, w;
u = read(); v = read(); w = read();
add(u, v, w); add(v, u, w);
}
SPFA();
printf("%d",dis[end][k]);
return 0;
}
【luogu P4568 [JLOI2011]飞行路线】 题解的更多相关文章
- 洛谷 P4568 [JLOI2011]飞行路线 题解
P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ...
- luogu P4568 [JLOI2011]飞行路线
传送门 看到免费次数\(k\)最多只有10,可以考虑构建\(k+1\)层的分层图,即每一层正常连边,上下两层对应点连边权为0的单向边,最后对所有层里面的\(di_t\)取\(\max\)救星了 #in ...
- 洛谷 P4568 [JLOI2011]飞行路线 解题报告
P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为0到\(n−1\ ...
- [JLOI2011]飞行路线 题解
[JLOI2011]飞行路线 题解 题目TP门 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有 ...
- 洛谷 P4568 [JLOI2011]飞行路线
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
- [洛谷P4568][JLOI2011]飞行路线
题目大意:最短路,可以有$k$条边无费用 题解:分层图最短路,建成$k$层,层与层之间的边费用为$0$ 卡点:空间计算出错,建边写错 C++ Code: #include <cstdio> ...
- Luogu 2939 [USACO09FEB]改造路Revamping Trails && Luogu 4568 [JLOI2011]飞行路线
双倍经验 写这两题之前被大佬剧透了呜呜呜. 分层图+最短路. 因为有$k$次机会能够把路径的费用变为$0$,我们可以建$k + 1$层图,对于每一层图我们把原来的边权和双向边连到上面去,而对于层与层之 ...
- P4568 [JLOI2011]飞行路线 分层图最短路
思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ...
- [JLOI2011]飞行路线题解
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
随机推荐
- Spark 1.6.2 + Beam 2.0.0读取Mongodb数据进行相应逻辑处理
不多说,直接上干货! http://blog.csdn.net/jianglushou9763/article/details/73332805 如果需要 APACHE BEAM2.0.0版本如何支持 ...
- 【c#文档】在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
[c#文档]https://msdn.microsoft.com/zh-cn/library/system.convert.toint32.aspx 转载自:http://www.cnblogs.co ...
- java的访问修饰符
Java中通过访问控制符(default,private,public,protected)来控制对类.变量.方法.构造方法的访问. 下表说明了4中修饰符的访问权限: 修饰符 当前类 同一包内 子孙类 ...
- [转]批处理for命令使用指南
摘要:本文由浅入深,为大家专门讲解for的用法,希望大家喜欢. 首先应该明确的是,for不是一个简单的命令,它的用法比较复杂,它还可以带四个参数(/L /D /R /F),其中:/L和/F参数是最经常 ...
- Coursera 机器学习 第9章(上) Anomaly Detection 学习笔记
9 Anomaly Detection9.1 Density Estimation9.1.1 Problem Motivation异常检测(Density Estimation)是机器学习常见的应用, ...
- JavaScript中有var和没var的区别
Js中的变量声明的作用域是以函数为单位,所以我们经常见到避免全局变量污染的方法是 (function(){ // ... })(); 在函数内部,有var和没var声明的变量是不一样的.有var声明的 ...
- 阻止事件的默认行为,例如click <a>后的跳转~
在W3C中,使用preventDefault()方法: 在IE中,使用window.event.returnValue = false.
- ACdream 1098——圆有点挤——————【数学计算】
圆有点挤 Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Status Pr ...
- Linux Kernel文件系统写I/O流程代码分析(一)
Linux Kernel文件系统写I/O流程代码分析(一) 在Linux VFS机制简析(二)这篇博客上介绍了struct address_space_operations里底层文件系统需要实现的操作 ...
- Android活动的启动模式
1. standard 标准模式,是活动默认的启动模式,在不进行显示指定的情况下,所有活动都会自动使用这种模式. Android使用返回栈管理活动,在standard模式下,每当启动一个新的活动,它就 ...