题目链接: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]飞行路线】 题解的更多相关文章

  1. 洛谷 P4568 [JLOI2011]飞行路线 题解

    P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ...

  2. luogu P4568 [JLOI2011]飞行路线

    传送门 看到免费次数\(k\)最多只有10,可以考虑构建\(k+1\)层的分层图,即每一层正常连边,上下两层对应点连边权为0的单向边,最后对所有层里面的\(di_t\)取\(\max\)救星了 #in ...

  3. 洛谷 P4568 [JLOI2011]飞行路线 解题报告

    P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为0到\(n−1\ ...

  4. [JLOI2011]飞行路线 题解

    [JLOI2011]飞行路线 题解 题目TP门 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有 ...

  5. 洛谷 P4568 [JLOI2011]飞行路线

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  6. [洛谷P4568][JLOI2011]飞行路线

    题目大意:最短路,可以有$k$条边无费用 题解:分层图最短路,建成$k$层,层与层之间的边费用为$0$ 卡点:空间计算出错,建边写错 C++ Code: #include <cstdio> ...

  7. Luogu 2939 [USACO09FEB]改造路Revamping Trails && Luogu 4568 [JLOI2011]飞行路线

    双倍经验 写这两题之前被大佬剧透了呜呜呜. 分层图+最短路. 因为有$k$次机会能够把路径的费用变为$0$,我们可以建$k + 1$层图,对于每一层图我们把原来的边权和双向边连到上面去,而对于层与层之 ...

  8. P4568 [JLOI2011]飞行路线 分层图最短路

    思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ...

  9. [JLOI2011]飞行路线题解

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

随机推荐

  1. 简单的CSS3鼠标滑过图片标题和遮罩层动画特效

    此文转自:http://www.cnblogs.com/w2bc/p/5735300.html,仅供本人学习参考,版权归原作者所有!   这是一款使用CSS3制作的简单的鼠标滑过图片标题和遮罩层动画特 ...

  2. TOJ 1258 Very Simple Counting

    Description Let f(n) be the number of factors of integer n. Your task is to count the number of i(1 ...

  3. mysql 数据库8.0版本,jdbc驱动连接问题

    前言 8.0版本的mysql数据的连接 与 5.0的有所不同,下面直接贴出  8.0版本应该有的 jdbc驱动连接,还有 mysql 的jdbc jar包要8.0以上的 内容如下 : jdbc.dri ...

  4. centos6.5下搭建oracle 11g

    1.安装依赖 sudo yum install binutils compat-libstdc++-33 compat-libstdc++-33.i686 elfutils-libelf elfuti ...

  5. APP测试点集合

    一.功能性测试: (1)根据产品需求文档编写测试用例 (2)软件设计文档编写用例 二.兼容性适配性测试: (1)Android.iOS版本的兼容性 (2)手机分辨率兼容性 (3)网络的兼容性:2G/3 ...

  6. Oracle 过程中变量赋值

    create or replace function get_sal1(id employees.employee_id%type) return number is sal employees.sa ...

  7. c# 判断是否是DICOM文件

    public bool isDicom(string filename) { FileStream fs = File.OpenRead(filename); ]; fs.Read(data, , d ...

  8. 弹性布局学习-详解flex-wrap(五)

    目录 弹性布局学习-介绍(一)  弹性布局学习-详解 flex-direction[决定主轴的方向](二) 弹性布局学习-详解 justify-content(三) 弹性布局学习-详解 align-i ...

  9. 转载:.NET Memory Leak: XmlSerializing your way to a Memory Leak

    原文地址:http://blogs.msdn.com/b/tess/archive/2006/02/15/532804.aspx I hate to give away the resolution ...

  10. javaSE练习2——流程控制_2.1

    一.企业发放的奖金根据利润提成.利润低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40万之间时,高 ...