题目链接: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. 触发Full GC的时机

    由于Full GC的耗时是Minor GC的十倍左右,所以Full GC的频率设计得比Minor GC低得多.现总结一下触发Full GC的情况. 在那些实现了CMS的比较新的虚拟机中,如果配置了-X ...

  2. Django api

    http://www.cnblogs.com/wulaoer/p/5276050.html

  3. 在 Azure 中创建静态 HTML Web 应用

    Azure Web 应用提供高度可缩放.自修补的 Web 托管服务. 本快速入门教程演示如何将基本 HTML+CSS 站点部署到 Azure Web 应用. 使用 Azure CLI 创建 Web 应 ...

  4. git pull冲突报错

    修改代码后在git pull报错: error: Your local changes to the following files would be overwritten by merge: xx ...

  5. FreeSwitch无法启动,提示进程pid锁定的解决方法

    来源:https://stackoverflow.com/questions/12817232/how-do-i-call-a-local-softphone-on-freeswitch error ...

  6. Android4.4 在Framework新增内部资源编译不过的问题

    如果在Frameworks新增内部资源,并在Java代码中使用类似形式来引用资源:com.android.internal.R.layout.xxx,需要在frameworks/base/core/r ...

  7. 通过tomcat shutdown port关闭tomcat

    在tomcat server.xml配置文件中,有个配置项 <Server port="8005" shutdown="SHUTDOWN"> 通过向 ...

  8. Sass学习笔记(三)

    一.Sass的控制命令 二.Sass的函数功能 sass中除了可以定义变量,还自备了一系列函数功能,主要包括:字符串函数.数字函数.列表函数.颜色函数.Instrospection函数.三元函数等.当 ...

  9. 多线程篇四:ThreadLocal实现线程范围内变量共享

    1.static实现线程范围内变量共享 package com.test.shareData; import java.util.Random; /** * 多线程范围内的数据共享 * @author ...

  10. 详解nodejs中使用socket的私聊和公聊的办法

    详解nodejs中使用socket的私聊和公聊的办法 nodejs的应用中,关于socket应该是比较出彩的了,socket.io在github上有几万人的star,它的成功应该是不输于express ...